第1个回答 2012-02-12
// 连接数据库:
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=Products"; // sql server数据库的URL地址(oracle的不一样)
String userName = "sa"; // 用户名
String userPwd = "123"; // 密码
Connection dbConn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
try {
Class.forName(driverName);// 加载JDBC驱动
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
String sql = "select pro.product_id from Products pro where pro.customer_id = ?"
String customerId = "0001"; //假设客户id是string类型的
psmt = dbConn.prepareStatement(sql);//预编译sql语句
psmt.setString(1, customerId); // 给第一个问号赋值
rs = psmt.executeQuery() ;
String[] productIds = (String[]) rs.getArray(“product_id”).getArray(); //将查询出来的产品id放入数组之中,假设产品id都是string类型的
} finnally {
if (rs != null) {
rs.close();
}
if(psmt != null) {
psmt.close();
}
if(dbConn!= null) {
dbConn .close();
}
}