package com.yanyu;
import java.sql.*;
import java.util.ResourceBundle;
public class JDBCTest01 {
// 第三次作业
// 读取配置文件
// psvm
// = 右边有了 ,左边的 ; crtl alt v
public static void main(String[] args) {
// RB 获取 资源绑定对象
// = 右边有了 ,左边的 ; crtl alt v
ResourceBundle bundle = ResourceBundle.getBundle("com/yanyu/db");//ctrl
// ; 后面 ctrl alt v
// ba .properties 删掉
// 用资源绑定对象 读取文件 bundle 的 前两个字母 bu
String driver = bundle.getString("driver");// ctrl alt v
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
// String driver1 = bundle.getString("driver");// ctrl alt v
// 放大作用域
Connection con = null;
// int num1 = 10;
PreparedStatement ps = null;
ResultSet rs = null;
// 注册驱动
try {
Class.forName(driver);
// 获取链接 对象
con = DriverManager.getConnection(url, user, password);
// 关闭事务提交
con.setAutoCommit(false);
// 操作对象
ps = con.prepareStatement("insert into user values(?,?)");//预编译
ps.setInt(1,10);
ps.setString(2,"yy");
ps.executeUpdate();
con.commit();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
if (con != null) {
try {
con.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
throw new RuntimeException(e);
}finally {
// 关闭数据流
// rs ps con
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
// alt enter
}
}
22计应04
最新推荐文章于 2024-10-15 11:18:59 发布