获取数据连接
不使用Druid封装
package Utils;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
static String url;
static String password;
static String driver;
static String user;
static {
try {
Properties info = new Properties();
info.load(new FileInputStream("src\\jdbc.properties"));
url = info.getProperty("url");
user = info.getProperty("username");
driver = info.getProperty("driverClassName");
password = info.getProperty("password");
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception{
return DriverManager.getConnection(url, user, password);
}
public static void close(Statement st,Connection conn){
if(st != null){
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close(ResultSet rs,Statement ps,Connection conn){
if(rs != null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
使用Druid封装
package Utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtilsByDruid {
static DataSource ds;
static{
try {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\JDBC.properties"));
ds = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception {
return ds.getConnection();
}
public static void close(ResultSet rs,Statement ps, Connection conn){
if(rs != null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close(Statement st,Connection conn){
if(st != null){
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
封装怎删改查方法
package Utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class CRUDUtils {
public static int update(String sql,Object... params){
Connection connection = null;
PreparedStatement ps = null;
try {
connection = JDBCUtilsByDruid.getConnection();
ps = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1,params[i]);
}
int update = ps.executeUpdate();
return update;
} catch (Exception e) {
throw new RuntimeException();
}finally {
JDBCUtilsByDruid.close(ps,connection);
}
}
public static user queryString(String sql,Object... params) {
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = JDBCUtilsByDruid.getConnection();
ps = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1,params[i]);
}
rs = ps.executeQuery();
if(rs.next()){
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
String email = rs.getString("email");
user user = new user(id,username,password,email);
return user;
}
return null;
} catch (Exception e) {
throw new RuntimeException();
} finally {
JDBCUtilsByDruid.close(rs,ps,connection);
}
}
public static List<user> queryAll(String sql, Object... params) {
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = JDBCUtilsByDruid.getConnection();
ps = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1,params[i]);
}
rs = ps.executeQuery();
List<user> list = new ArrayList<>();
while(rs.next()){
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
String email = rs.getString("email");
user user = new user(id,username,password,email);
list.add(user);
}
return list;
} catch (Exception e) {
throw new RuntimeException();
} finally {
JDBCUtilsByDruid.close(rs,ps,connection);
}
}
}
数据库user表
package Utils;
public class user {
private Integer id;
private String username;
private String password;
private String email;
public user() {
}
public user(Integer id, String username, String password, String email) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "user{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
'}';
}
}