package com.cx;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* 使用封装工具
*/
public class jdbcTest2 {
//向Department表中添加一条数据
public void insertDeparments(String department_name,int location_id){
Connection conn = null;
Statement state = null;
try {
//使用封装工具
conn = jdbcUtil1.getConnection();
String sql="insert into departments values(default,'"+department_name+"',"+location_id+")";
state = conn.createStatement(); //通过conn对象调用Connection接口下createStatement方法
int flag = state.executeUpdate(sql);
System.out.println(flag);
} catch (Exception e) {
e.printStackTrace();
}finally {
jdbcUtil1.closeConnection(conn);
jdbcUtil1.closeStatment(state);
}
}
//更新departments表中的department_id为6的数据,将部门名称修改为教学不,location_id修改为6
public void updateDepartments(String department_name,int location_id,int department_id){
Connection conn = null;
Statement state = null;
try {
conn= jdbcUtil1.getConnection();
state = conn.createStatement();
String sql = "update departments d set d.department_name = '"+department_name+"',d.location_id ="+location_id+" where d.department_id ="+department_id+"";
int flag = state.executeUpdate(sql);
System.out.println(flag);
}catch (Exception e){
e.printStackTrace();
}finally {
jdbcUtil1.closeConnection(conn);
jdbcUtil1.closeStatment(state);
}
}
//查询Departments表中部门ID为6的部门信息
public void selectDepartmentsById(int departmentId){
Connection conn = null;
Statement state = null;
ResultSet rs = null;
try {
conn = jdbcUtil1.getConnection();
state = conn.createStatement();
String sql = "select * from departments d where d.department_id = "+departmentId;
//执行查询返回结果
rs = state.executeQuery(sql);
while (rs.next()){
int a = rs.getInt("department_id");
String s = rs.getString("department_name");
int b = rs.getInt(3);
System.out.println(a+" "+s+" "+b);
}
}catch (Exception e){
e.printStackTrace();
}finally {
jdbcUtil1.closeConnection(conn);
jdbcUtil1.closeStatment(state);
jdbcUtil1.closeResultSet(rs);
}
}
public static void main(String[] args) {
jdbcTest2 test2 = new jdbcTest2();
//test.insertDeparments("工程部",6);
// test.updateDepartments("研发",6,8);
test2.selectDepartmentsById(6);
}
}
package com.cx;
import java.sql.*;
import java.util.ResourceBundle;
/**
* 封装jdbc工具类
*/
public class jdbcUtil1 {
private static String driver;
private static String jdbcUrl;
private static String username;
private static String userpassword;
static {
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
driver=bundle.getString("driver");
jdbcUrl= bundle.getString("jdbcUrl");
username=bundle.getString("username");
userpassword=bundle.getString("userpassword");
try {
Class.forName(driver);
}catch (Exception e){
e.printStackTrace();
}
}
//获取Connection对象
public static Connection getConnection() {
Connection conn = null;
try {
//获取连接
conn = DriverManager.getConnection(jdbcUrl,username,userpassword);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//关闭Statement
public static void closeStatment(Statement state){
try {
if (state != null){
state.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
//关闭Connection
public static void closeConnection(Connection conn){
try {
if (conn != null){
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
//关闭
public static void closeResultSet(ResultSet rs){
try {
if (rs != null){
rs.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
driver="com.mysql.jdbc.Driver"
jdbcUrl ="jdbc:mysql://localhost:3306/caixx?useUnicode=true&usecharacterEncoding=utf-8&useSSL=false"
username="root"
userpassword="root"
发现使用properties就会报错

不使用properties就正常, IDEA到底如何使用这个?