日期:2014-05-20 浏览次数:20919 次
package com.i;
import java.sql.*;
public class Test{
public static void main(String[] args){
Connection ct = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
//加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SqlServerDriver");
//建立连接
ct = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=userManageSystemDb","sa","123456");
ps = ct.prepareStatement("SELECT * FROM tb_user");
rs = ps.executeQuery();
while(rs.next()){
int id = rs.getInt("id");
String userName = rs.getString("userName");
String password = rs.getString("password");
int grade = rs.getInt("grade");
System.out.println(id+","+userName+","+password+","+grade);
}
}
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();
}
}
}
}
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SqlServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at com.i.Test.main(Test.java:15)