日期:2014-05-20  浏览次数:20826 次

连接数据库会有这种错误?
/**
 * microsoft的JDBC的连接数据库方式
 */
import java.sql.*;

public class Test2 {
static Connection ct = null;
static PreparedStatement ps = null;
static ResultSet rs = null;

public static void main(String[] args){

try {
// 1.加在驱动
Class.forName("com.microsoft.jdbc.sqlServer.SQLServerDriver");
 
// 2.得到连接
ct = DriverManager.getConnection
("jdbc:microsoft:sqlserver://localhost:1433; databaseName=ppp", "sa", "sa"); 

ps = ct.prepareStatement("select * from TABLE1");
rs = ps.executeQuery();
while (rs.next()) {
int i = rs.getInt(1);
String s1 = rs.getString(2);
String s2 = rs.getString(3);
System.out.println(i + " " + s1 + " " + s2);

}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (ct != null) {
ct.close();
}
} catch (Exception e) {
e.printStackTrace();
}
// TODO: handle exception
}

}

}


运行提示:java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;databaseName=ppp 找不到合适的驱动是什么问题,连接数据库的三个jar包我都已经导入过工程里面了,用的是sqlserver2000,打过SP4补丁。

------解决方案--------------------
1、首先确定你导入的jar跟你的数据库是否匹配

2、看这样改行不行
// 1.加在驱动
Class.forName("com.microsoft.sqlServer.jdbc.SQLServerDriver");
  
// 2.得到连接
ct = DriverManager.getConnection
("jdbc:sqlserver://localhost:1433; databaseName=ppp", "sa", "sa");

3、如果还不行,你在工程上右键 buildpath ->configure build path 在切换到librarys选项卡看看导入驱动jar是否正确。