做業二

     基於java+mysql實現JDBC對學生信息進行增刪改查java

 鏈接數據庫mysql

# db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/menagerie?serverTimezone=UTC
jdbc.user=root
jdbc.password=0000
initSize=1
maxActive=2sql

 

 

工具類DBUtil(對jdbc進行封裝)數據庫

package util;apache

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;併發

import org.apache.commons.dbcp.BasicDataSource;工具

/**
* 鏈接池版本的數據庫鏈接管理工具類
* 適合於併發場合
* @author LvChaoZhang
*
*/
public class DbUtils {
private static String driver;
private static String url;
private static String username;
private static String password;
private static int initSize;
private static int maxActive;
private static BasicDataSource bs;
static {
//鏈接池
bs=new BasicDataSource();
Properties cfg=new Properties();
try {
InputStream in =DbUtils.class.getClassLoader().getResourceAsStream("db.properties");
cfg.load(in);
//初始化參數
driver=cfg.getProperty("jdbc.driver");
url=cfg.getProperty("jdbc.url");
username=cfg.getProperty("jdbc.user");
password=cfg.getProperty("jdbc.password");
initSize=Integer.parseInt(cfg.getProperty("initSize"));
maxActive=Integer.parseInt(cfg.getProperty("maxActive"));
in.close();
//初始化鏈接池
bs.setDriverClassName(driver);
bs.setUrl(url);
bs.setUsername(username);
bs.setPassword(password);
bs.setInitialSize(initSize);
bs.setMaxActive(maxActive);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static Connection getConnection() {
try {
//getConnection()從鏈接池中獲取重用的鏈接,若是鏈接池滿了,則等待,若是有鏈接歸還,則獲取重用的鏈接
Connection conn = bs.getConnection();
return conn;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException();
}

}
public static void rollback(Connection conn) {
if(conn!=null) {
try {
conn.rollback();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void close(Connection conn) {
if(conn!=null) {
try {
//將用過的鏈接歸還到鏈接池中
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}測試

 

dao:主要調用DBUtil操做相應的model——增刪改查url

 

package dao;
import java.util.List;server

import entity.Student;
//學生接口類
public interface StudentDao {
//抽象方法,查詢學生,返回整個學生
public List<Student> findStudent();
//保存學生,返回值爲空,傳進去須要保存的學生信息
public void save(Student stu);
//經過stuId查詢學生的信息,返回學生對象
public Student findById(int stuId);
//修改操做的更新操做,傳進來一個學生對象,無返回值
public void updateStudent(Student student);
//刪除操做,根據stuId來刪除
public void deleteStudent(int stuId);
}

 

 

 

 

 

package dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import entity.Student;
import sun.management.snmp.util.SnmpListTableCache;
import util.DbUtils;

public class StudentDaoImpl implements Serializable, StudentDao {

/**
*
*/
private static final long serialVersionUID = 1L;

public List<Student> findStudent() {
Connection conn=null;
try {
//獲取鏈接
conn=DbUtils.getConnection();
//執行查詢語句
String sql="select * from Student";
Statement sm = conn.createStatement();
//得到查詢結果
ResultSet rs = sm.executeQuery(sql);
//生成list集合用來存儲student對象
List<Student> list=new ArrayList<Student>();
while(rs.next()) {
//生成student對象
Student student=new Student();
//存儲學生的各個信息
student.setStuId(rs.getInt("stuId"));
student.setStuName(rs.getString("stuName"));
student.setStuSex(rs.getString("stuSex"));
student.setStuTel(rs.getString("stuTel"));
student.setStuDisc(rs.getString("stuDisc"));
student.setScore(rs.getString("score"));
//將學生存入集合中
list.add(student);
}
return list;
} catch (Exception e) {
throw new RuntimeException("找不到學生",e);
}finally {
//關閉鏈接
DbUtils.close(conn);
}
}
//保存學生信息
public void save(Student stu) {
Connection conn=null;
try {
//獲取鏈接
conn=DbUtils.getConnection();
String sql="insert into student values(?,?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, stu.getStuId());
ps.setString(2, stu.getStuName());
ps.setString(3, stu.getStuSex());
ps.setString(4, stu.getStuTel());
ps.setString(5, stu.getStuDisc());
ps.setString(6, stu.getScore());
ps.executeUpdate();//執行插入語句
} catch (Exception e) {
throw new RuntimeException("沒法保存學生信息",e);
}finally {
DbUtils.close(conn);
}
}

//經過stuId來查詢學生
public Student findById(int stuId) {
Connection conn=null;

try { conn=DbUtils.getConnection(); String sql="select *from student where stuId=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, stuId); //執行sql語句 ResultSet rs = ps.executeQuery(); Student student=new Student(); if(rs.next()) { student.setStuId(rs.getInt("stuId")); student.setStuName(rs.getString("stuName")); student.setStuSex(rs.getString("stuSex")); student.setStuTel(rs.getString("stuTel")); student.setStuDisc(rs.getString("stuDisc")); student.setScore(rs.getString("score")); } return student; } catch (Exception e) { throw new RuntimeException("經過id查詢失敗",e); }finally { DbUtils.close(conn); } } //更新操做 public void updateStudent(Student student) { Connection conn=null; try { conn=DbUtils.getConnection(); String sql="update student set stuName=?,stuSex=?,stuTel=?,stuDisc=?,score=? where stuId=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, student.getStuName()); ps.setString(2, student.getStuSex()); ps.setString(3, student.getStuTel()); ps.setString(4, student.getStuDisc()); ps.setInt(5, student.getStuId()); ps.setString(6, student.getScore()); //更新操做 ps.executeUpdate(); } catch (Exception e) { throw new RuntimeException("更新失敗",e); }finally { DbUtils.close(conn); } } //刪除操做 public void deleteStudent(int stuId) { Connection conn=null; try { conn=DbUtils.getConnection(); String sql="delete from student where stuId=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, stuId); //執行操做 ps.executeUpdate(); } catch (Exception e) { throw new RuntimeException("刪除失敗",e); }finally { DbUtils.close(conn); } } /*測試刪除方法 public static void main(String[] args) { StudentDao dao=new StudentDaoImpl(); dao.deleteStudent(2018004); } */ /*測試經過stuId方法 public static void main(String[] args) { StudentDao dao=new StudentImpl(); Student student = dao.findById(2018001); System.out.println(student.getStuName()); } */ /*測試保存 public static void main(String[] args) { StudentDao dao=new StudentImpl(); Student stu=new Student(); stu.setStuId(2018003); stu.setStuName("小花"); stu.setStuSex("F"); stu.setStuTel("123"); stu.setStuDisc("小花是個好姑娘"); dao.save(stu); } */ /*測試一下 public static void main(String[] args) { StudentDao dao=new StudentImpl(); List<Student> students = dao.findStudent(); for(Student student:students) { System.out.println(student.getStuName()); } } */ }

相關文章
相關標籤/搜索