import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) {
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //驱动的名字
String url = "jdbc:sqlserver://127.0.0.1:1433; databaseName=db_JXC"; //sqlServer所占用端口和所要连接的数据库名字
String user = "sa"; //sqlServer用户名
String password = ""; //密码啊
try {
Class.forName(driver); //加载驱动
Connection conn = DriverManager.getConnection(url, user, password);//建立到TestDB数据库的连接
if(!conn.isClosed()) //如果开着
System.out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement(); //建立sql语句的执行容器
String sql = "select * from tb_userlist";
ResultSet rs = statement.executeQuery(sql); //执行该sql语句,返回resultSet对象
String name = null;
while(rs.next()) { //分析resultSet对象,确定用户是否存在
name = rs.getString("name");
System.out.println(name);
}
rs.close();
conn.close();
}catch(ClassNotFoundException e) {
System.out.println("Sorry, can't find the Driver");
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
}
其中需要引用的包sqljdbc4.jar