读取properties文件
工具类代码
package com.lin;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
/**
* jdbc 工具类
*/
public class jdbcUtil {
private static String driver ;
private static String jdbcUrl ;
private static String userName ;
private static String password ;
static {
//读取properties文件
ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");
driver = jdbc.getString("driver");
jdbcUrl =jdbc.getString("jdbcUrl");
userName=jdbc.getString("userName");
password=jdbc.getString("userName");
try {
Class.forName("driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取Connection 对象
public static Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(jdbcUrl, userName, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
//关闭Statement
public static void closeStatement(Statement statement) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭Connection
public static void closeConnection(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//关闭所有资源
public static void closeResource(Statement statement, Connection connection) {
closeStatement(statement);
closeConnection(connection);
}
}
测试类代码
package com.lin;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 使用工具类进行jdbc操作
*/
public class jdbcTest1 {
//向Departments 表中添加一个数据
public void insertDepartments(String department_name,int location_id) {
Connection connection = null;
Statement statement = null;
try {
//1.注册驱动
connection = jdbcUtil.getConnection();
//编辑sql语句
String sql = "insert into departments values(default,'" + department_name + "','" + location_id + "')";
//创建Statement对象用于发送数据
statement = connection.createStatement();
int flag = statement.executeUpdate(sql);
System.out.println(flag);
} catch (Exception e) {
e.printStackTrace();
} finally {
jdbcUtil.closeStatement(statement);
jdbcUtil.closeConnection(connection);
}
}
//更新departments 表中的department_di 为6 的数据 部门名称修改为java研发部 location_id改为6
public void updateDepartments( String department_id,String department_name, int location_id){
Connection connection = null;
Statement statement = null;
try {
connection= jdbcUtil.getConnection();
//创建Statement对象用于发送数据
statement =connection.createStatement();
//创建SQL语句
String sql = "update departments d set d.department_name = '"+department_name+"',d.location_id = "+location_id+" where d.department_id ="+department_id;
int falg = statement.executeUpdate(sql);
if (falg ==1){
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
jdbcUtil.closeResource(statement,connection);
}
}
public static void main(String[] args) {
com.lin.jdbcTest jdbcTest = new com.lin.jdbcTest();
jdbcTest.insertDepartments("采购部",12);
// jdbcTest.updateDepartments("7","推广销售部",7);
}
}
如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!
加入交流群
请使用微信扫一扫!