当前位置:Gxlcms > 数据库问题 > JDBC基本语法

JDBC基本语法

时间:2021-07-01 10:21:17 帮助过:2人阅读

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基本语法

标签:

人气教程排行