package com.autumn.jdbc.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
private static final String DRIVERNAME= "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/shixun?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
private static final String USER = "root";
private static final String PASSWORD = "123456";
private static Connection conn;
public static Connection getConn(){
try {
Class.forName(DRIVERNAME);
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return conn;
}
}
package com.autumn.jdbc.dao.impl;
import com.autumn.jdbc.dao.ClassInfoDao;
import com.autumn.jdbc.entity.ClassInfo;
import com.autumn.jdbc.utils.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ClassInfoDaoImpl implements ClassInfoDao {
@Override
public List<ClassInfo> findAll() {
List<ClassInfo> lists = new ArrayList<>();
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
ResultSet resultSet = null;
try {
ps = conn.prepareStatement("select * from shixun.classinfo");
resultSet = ps.executeQuery();
while (resultSet.next()){
lists.add(new ClassInfo(resultSet.getInt(1),resultSet.getString(2)));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if (resultSet!=null){
resultSet.close();
}
if (ps!=null){
ps.close();
}
if (conn!=null){
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return lists;
}
@Override
public ClassInfo findById(int classId) {
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
ResultSet resultSet = null;
ClassInfo classInfo = null;
try {
ps = conn.prepareStatement("select * from shixun.classinfo where classId = ?");
ps.setInt(1,classId);
resultSet = ps.executeQuery();
if (resultSet.next()){
classInfo = new ClassInfo(resultSet.getInt(1), resultSet.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if (resultSet!=null){
resultSet.close();
}
if (ps!=null){
ps.close();
}
if (conn!=null){
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return classInfo;
}
@Override
public int add(ClassInfo classInfo) {
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
ResultSet resultSet = null;
int n = 0;
try {
ps = conn.prepareStatement("insert into shixun.classInfo values(null,?)");
ps.setString(1,classInfo.getClassName());
n = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if (resultSet!=null){
resultSet.close();
}
if (ps!=null){
ps.close();
}
if (conn!=null){
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return n;
}
@Override
public int delete(int classId) {
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
ResultSet resultSet = null;
int n = 0;
try {
ps = conn.prepareStatement("delete from shixun.classInfo where classId = ?");
ps.setInt(1,classId);
n = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if (resultSet!=null){
resultSet.close();
}
if (ps!=null){
ps.close();
}
if (conn!=null){
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return n;
}
@Override
public int update(ClassInfo classInfo) {
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
ResultSet resultSet = null;
int n = 0;
try {
ps = conn.prepareStatement("update shixun.classInfo set className = ? where classId = ?");
ps.setString(1,classInfo.getClassName());
ps.setInt(2,classInfo.getClassId());
n = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if (resultSet!=null){
resultSet.close();
}
if (ps!=null){
ps.close();
}
if (conn!=null){
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return n;
}
}
package com.autumn.jdbc.dao;
import com.autumn.jdbc.entity.ClassInfo;
import java.util.List;
public interface ClassInfoDao {
List<ClassInfo> findAll();
ClassInfo findById(int classId);
int add(ClassInfo classInfo);
int delete(int classId);
int update(ClassInfo classInfo);
}
package com.autumn.jdbc.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ClassInfo implements Serializable {
private int classId;
private String className;
public ClassInfo(String className) {
this.className = className;
}
}
package com.autumn.jdbc;
import com.autumn.jdbc.dao.ClassInfoDao;
import com.autumn.jdbc.dao.impl.ClassInfoDaoImpl;
import com.autumn.jdbc.entity.ClassInfo;
import java.util.List;
public class Test {
public static void main(String[] args) {
}
}