码迷,mamicode.com
首页 > 数据库 > 详细

jdbc总结

时间:2017-05-28 17:23:45      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:username   catch   tac   gen   dex   host   set   cep   drive   

Connection con = null;
PreparedStatement sta = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("反射加载驱动程序错误!");
}
try {
con = DriverManager
.getConnection(
"jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8",
"root", "");
} catch (SQLException e) {
System.out.println("获取数据库链接错误!");
}

String sql = "SELECT * FROM user WHERE username=?";
try {
sta = con.prepareStatement(sql);
sta.setString(1, "王五");
} catch (SQLException e1) {
System.out.println("预编译语句错误!");
}
try {
rs = sta.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("id") + " "
+ rs.getString("username"));

}
} catch (SQLException e1) {
System.out.println("结果集错误!");

} finally {

if (rs != null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (sta != null)
try {
sta.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

jdbc总结

标签:username   catch   tac   gen   dex   host   set   cep   drive   

原文地址:http://www.cnblogs.com/1994chen/p/6916058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!