import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 测试jdbc类
*
* @author Administrator
*
*/
public class TestJdbc {
public static void main(String[] args) {
try {
// 加载驱动类
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 连接的地址
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=NorthWind";
// 用户名
String user = "sa";
// 密码
String password = "910627";
// 连接接口
Connection conn = DriverManager.getConnection(url, user, password);
// 建立执行sql语句的对象
Statement st = conn.createStatement();
String query = "select top (10) orderId from Orders";
// 执行sql语句 返回结果集
ResultSet rs = st.executeQuery(query);
// 读取结果
while (rs.next()) {
System.out.println(rs.getString("orderId"));
}
// 关闭连接
rs.close();
st.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("加载驱动失败");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("连接数据库失败");
e.printStackTrace();
}
}
}