/**
* 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补丁。
* 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.加在驱动
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是否正确。
而且我觉得很eclipse有时候很诡异,我昨天也照上面尝试了不行。今天居然可以
最不诡异的就是程序了,有哪一个IDE不是有大堆的设置,这些参数,往往又跟很多原理性的东西有联系,我想如果你更多的弄清楚来龙去脉,就不会说诡异这个词了