import java.sql.*;
- 注册驱动(不同的数据库驱动不同)
Class.forName("com.ibm.db2.jcc.DB2Driver");
- 创建连接对象
Connection conn = DriverManager.getConnection(url,username,password);
- 创建预编译命令对象
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(x,y); //给第x个占位符?赋值为y
- 执行SQL
//查询sql,返回查询的结果集对象
ResultSet rs = pstmt.execteQuery();
//增删改sql,返回受影响的行数
int line = pstmt.execteUpdate();
- 输出结果集数据
while(rs.next){
System.out.println(rs.getObject("column_name"));
}
- 关闭结果集对象、命令对象、连接对象
rs.close();
stmt.close();
conn.close();
4429



被折叠的 条评论
为什么被折叠?



