Class.forName("com.mysql.jdbc.Driver");
2.获取连接
Connection conn=DriverMAnager.getConnection(url,username,password);
3.获取sql执行对象
PreapareStatment pst=conn.prepareStatement("sql");
4.执行sql
ResultSet rs=pst.executeQuery(); 查询
int i = pst.executeUpdate(); 增删改
5.查看执行结果
rs
i
6.关闭资源
public static void closeDb(ResultSet rs,PreparedStatement pst,Connection connection){
if(null != rs){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null != pst){
try {
pst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null != connection){
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本文介绍了一个典型的Java应用程序中对MySQL数据库进行操作的过程,包括加载驱动、建立连接、执行SQL语句及结果处理等步骤。
2126

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



