java.sql.SQLException: No suitable driver



找了N久未解决,在群里经过几个牛人们指点终于搞点,现总结如下



1 驱动包用的不对



2 连接串的写法不同于2000







//JDBC for SQL server 2000写法:

String DBDRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

//JDBC for SQL server 2005写法:


String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";








2003 写法

access.url=jdbc:microsoft:sqlserver://192.168.1.136:1433;DatabaseName=GameDB

2005 写法

String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=dbName";






附用的配置文件(2005)



-------------



drivers=com.microsoft.sqlserver.jdbc.SQLServerDriver

logfile=./logs/dblog.txt

access.maxconn=50

access.url=jdbc:sqlserver://192.168.1.136:1433;DatabaseName=Game23DB

access.user=sa

access.password=hujie


-------附属性文件建立及dbcontect-----


用配置文件连接数据库
1. Eclipse中,在src目录下建立db.properties文件,在里面配置数据库连接所需的 Driver,url,user,possword ,注意等号左右不能空格

如:
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;databaseName=restrant
user=sa
password=110 2.写一个数据库连接类
import java.io.IOException;
import java.sql.*;
import java.util.Properties; public Connection getCon() {
Connection con = null;
Properties properties = new Properties();
try {
properties.load(this.getClass().getResourceAsStream(
"/db.properties"));
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String password = properties.getProperty("password"); try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}

sql 2000 需要引用三个jar包 msbase.jar,mssqlserver.jar,msutil.jar



sql 2005 需要引用一个jar包 sqljdbc.jar