JDBC工具类抽取方式二(测试添加操作)
2018-03-28 05:20
417 查看
创建配置文件:
package cn.itheima.jdbc;
/**
* 提供获取连接和释放资源的方法
* @author XING
*
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class JDBCUtils_V2 {
private static String driver;
private static String url;
private static String username;
private static String password;
/**
* 静态代码块加载配置文件信息
*/
static{
ResourceBundle bundle = ResourceBundle.getBundle("db");
driver = bundle.getString("driver");
url = bundle.getString("url");
username = bundle.getString("username");
password = bundle.getString("password");
}
/**
* 获取连接方法
*
* @return
*/
public static Connection getConnection(){
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,username,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs){
if (rs !=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt !=null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn !=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package cn.itheima.jdbc;
/**
* 提供获取连接和释放资源的方法
* @author XING
*
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class JDBCUtils_V2 {
private static String driver;
private static String url;
private static String username;
private static String password;
/**
* 静态代码块加载配置文件信息
*/
static{
ResourceBundle bundle = ResourceBundle.getBundle("db");
driver = bundle.getString("driver");
url = bundle.getString("url");
username = bundle.getString("username");
password = bundle.getString("password");
}
/**
* 获取连接方法
*
* @return
*/
public static Connection getConnection(){
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,username,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs){
if (rs !=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt !=null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn !=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package cn.itheima.jdbc.test; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import org.junit.Test; import cn.itheima.jdbc.JDBCUtils_V1; import cn.itheima.jdbc.JDBCUtils_V2; /** * 测试工具类 * * @author XING * */ public class TestUtils { /** * 添加用户信息方法 */ @Test public void testAdd(){ Connection conn = null; PreparedStatement pstmt = null; try { //1.获取连接 conn = JDBCUtils_V2.getConnection(); //2.编写SQL语句 String sql = "insert into tbl_user values(null,?,?)"; //3.获取执行SQL语句对象 pstmt = conn.prepareStatement(sql); //4.设置参数 pstmt.setString(1, "lisi"); pstmt.setString(2, "hehe"); //5.执行插入操作 int row = pstmt.executeUpdate(); if(row>0){ System.out.println("添加成功"); }else{ System.out.println("添加失败"); } } catch (Exception e) { throw new RuntimeException(e); }finally{ //6.释放资源 JDBCUtils_V2.release(conn, pstmt, null); } }
相关文章推荐
- JDBC工具类抽取方式一(测试根据id查询)
- JDBC工具类抽取方式三(删除&修改操作)
- JDBC工具类抽取方式三(更新操作)
- 07_数据库创建,添加c3p0操作所需的jar包,编写c3p0-config.xml文件,编写User.java,编写jdbcUtils.java实现操作数据库的模板工具类,UserDao编写,Dao
- 07_数据库创建,添加c3p0操作所需的jar包,编写c3p0-config.xml文件,编写User.java,编写jdbcUtils.java实现操作数据库的模板工具类,UserDao编写,Dao
- java 演示使用jdbc-odbc桥连接的方式操作数据库 向数据库中添加数据
- Java jdbc操作oracle数据库的两种方式
- JDBC的简单封装,实现简单数据库操作工具类SQLHelper.
- Android 添加桌面快捷方式操作
- jdbc使用 Statement 接口实现添加数据操作小例子
- JDBC工具类抽取方法(使用properties配置文件)
- Servlet中利用JDBC操作数据库,往emp表中添加数据
- JDBC优化之路-2(添加工具类DBUtil)
- 《c++ 操作xml加载时添加元素简单测试小程序》
- Hadoop(四)Hive整合HBase及JDBC方式操作Hive
- JMeter添加测试数据是发生错误 no suitable driver found for JDBC
- 在Eclipse中测试MySQL-JDBC(15)c3p0配置文件对JDBC工具(JdbcUtil)类抽取 再优化
- JDBC实现用于操作数据库Mysql的工具类JDBCTools
- JDBC的批处理操作三种方式
- java JDBC(java 操作数据的方式)