java statement升级_Java -- JDBC 学习--通过Statement进行数据库更新操作

public classJDBCTools {public static voidrelease(ResultSet rs,

Statement statement, Connection conn) {if(rs != null){try{

rs.close();

}catch(SQLException e) {

e.printStackTrace();

}

}if (statement != null) {try{

statement.close();

}catch(Exception e2) {

e2.printStackTrace();

}

}if (conn != null) {try{

conn.close();

}catch(Exception e2) {

e2.printStackTrace();

}

}

}/*** 关闭 Statement 和 Connection

*@paramstatement

*@paramconn*/

public static voidrelease(Statement statement, Connection conn) {if (statement != null) {try{

statement.close();

}catch(Exception e2) {

e2.printStackTrace();

}

}if (conn != null) {try{

conn.close();

}catch(Exception e2) {

e2.printStackTrace();

}

}

}/*** 1. 获取连接的方法. 通过读取配置文件从数据库服务器获取一个连接.

*

*@return*@throwsException*/

public static Connection getConnection() throwsException {//1. 准备连接数据库的 4 个字符串.//1). 创建 Properties 对象

Properties properties = newProperties();//2). 获取 jdbc.properties 对应的输入流

InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");//3). 加载 2) 对应的输入流

properties.load(in);//4). 具体决定 user, password 等4 个字符串.

String user = properties.getProperty("user");

String password= properties.getProperty("password");

String jdbcUrl= properties.getProperty("jdbcUrl");

String driver= properties.getProperty("driver");//2. 加载数据库驱动程序(对应的 Driver 实现类中有注册驱动的静态代码块.)

Class.forName(driver);//3. 通过 DriverManager 的 getConnection() 方法获取数据库连接.

returnDriverManager.getConnection(jdbcUrl, user, password);

}/*** 通用的更新的方法: 包括 INSERT、UPDATE、DELETE

* 版本 1.*/

public voidupdate(String sql){

Connection conn= null;

Statement statement= null;try{

conn=getConnection();

statement=conn.createStatement();

statement.executeUpdate(sql);

}catch(Exception e) {

e.printStackTrace();

}finally{

release(statement, conn);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值