c3p0连接数据库失败No suitable driver

来源:互联网 发布:java编写杨辉三角 编辑:程序博客网 时间:2024/05/16 18:56

c3p0连接SQL 2005数据库报错No suitable driver,是url错误导致
这是jdbc连接数据库时配置
drivers=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1434;DatabaseName\=db_news2
这是c3p0连接数据库配置
dataSource.setJdbcUrl( “jdbc:sqlserver://localhost:1434; DatabaseName=db_news2”);
dataSource.setDriverClass( “com.microsoft.sqlserver.jdbc.SQLServerDriver”);
其中url不同

以下摘自互联网
测试代码
package database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.beans.PropertyVetoException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class SQLDB{
private static SQLDB dbPool;
private ComboPooledDataSource dataSource;

static {
dbPool=new SQLDB();
}

public SQLDB(){
try {
dataSource=new ComboPooledDataSource();
dataSource.setUser( “sa”);
dataSource.setPassword( “sa”);
dataSource.setJdbcUrl( “jdbc:sqlserver://localhost:1434; DatabaseName=db_news2”);
dataSource.setDriverClass( “com.microsoft.sqlserver.jdbc.SQLServerDriver”);
dataSource.setInitialPoolSize(2);
dataSource.setMinPoolSize(1);
dataSource.setMaxPoolSize(10);
dataSource.setMaxStatements(50);
dataSource.setMaxIdleTime(60);
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}

public final static SQLDB getInstance(){
System.out.println(“3”);
return dbPool;
}

public final Connection getConnection() {
try {
System.out.println(“4”);
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException( “无法从数据源获取连接 “,e);
}
}

public static void main(String[] args) throws SQLException {
Connection con = null;
try { System.out.println(“1”);
con = SQLDB.getInstance().getConnection();
System.out.println(“2”);
ResultSet rs = con.createStatement().executeQuery(“select * from newsusr”);
if(rs.next())
{
System.out.println(“执行结果”);
System.out.println(rs.getString(“userName”));
}
System.out.println(con);
} catch (Exception e){
} finally {
if (con != null)
con.close();
}
}
}

0 0