首页 > 数据库技术 > 详细

JDBC基本语法

时间:2016-05-08 10:18:09      阅读:214      评论:0      收藏:0      [点我收藏+]

JDBC基本操作步骤:

1,加载驱动,建立连接

2,执行SQL语句

3,关闭连接

建立连接:  

加载驱动

Class.forName("org.gjt.mm.mysql.Driver");

建立连接,localhost代表本机,3306是端口,five是数据库名,后面是编码集合数据库密码;
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/five?characterEncoding=utf-8","root","123456");

执行SQL语句

ps=con.prepareStatement(
"insert into t_product(productName,price,createTime)values(?,?,?)");

设置占位符:

ps.setString(1, bean.getProductName());
ps.setInt(2, bean.getPrice());
ps.setDate(3, bean.getBirthday());

更新数据库:

ps.executeUpdate();

抽象之后的关闭连接语法:

try {
if (re != null) {
re.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}

JDBC基本语法

原文:http://www.cnblogs.com/cj28-27/p/5469815.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!