1、JDBC单表记录的增删改查(20分)
已知:建立表student,并向表里插入几条记录,
create table student(
id int PRIMARY KEY auto_increment,
name varchar(20) not null,
schoolin date not null,
score float not null);
insert into student values(null,’李丽’,’2015-09-01’,86);
insert into student values(null,’王五’,’2016-09-01’,99);
insert into student values(null,’张三’,’2014-09-01’,88);
要求:用JAVA程序实现如下功能:
1、向表中增加记录并显示所有记录(数据自己指定);
2、从表中删除id=1的记录并显示所有记录;
3、修改表中记录:查询条件id=2,将name修改为:山东理工,修改完毕显示所有记录;
4、查询表中id=3的记录并显示;
Student类
public class Student { int id; String name, schoolin; float score; public Student() { super(); } public Student(int id, String name, String schoolin, float score) { super(); this.id = id; this.name = name; this.schoolin = schoolin; this.score = score; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", schoolin=" + schoolin + ", score=" + score + "]"; } }
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class BaseDao { Connection con; PreparedStatement pst; ResultSet rs; public Connection getConn() throws ClassNotFoundException, SQLException { // 1 加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 2 获得数据库的连接 Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3307/Student?useUnicode=true&characterEncoding=utf-8", "root", "usbw"); return con; } public void closeAll() throws SQLException { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } }
import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class StudentDao extends BaseDao { public int addStu(Student st) { int rst = 0; try { con = getConn(); String sql_insert = "insert into student(id, name, schoolin, score) values(null, ?, ?, ?)"; PreparedStatement pst = con.prepareStatement(sql_insert); pst.setString(1, st.name); pst.setString(2, st.schoolin); pst.setFloat(3, st.score); rst = pst.executeUpdate(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { closeAll(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rst; } public int deleteStu(int id) throws ClassNotFoundException, SQLException { con = getConn(); String sql = "delete from student where id=?"; PreparedStatement pst = con.prepareStatement(sql); pst = con.prepareStatement(sql); pst.setInt(1, id); int result = pst.executeUpdate(); closeAll(); return result; } public int updateStu(int id1, String name) { int age; String sql; int rst = 0; sql = "update student set name = ? where id = ?"; PreparedStatement pst; try { con = getConn(); pst = con.prepareStatement(sql); pst.setString(1, name); pst.setInt(2, id1); rst = pst.executeUpdate(); closeAll(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rst; } public List<Student> queryAll() throws SQLException, ClassNotFoundException { con = getConn(); List<Student> stu = new ArrayList<Student>(); String sql = "select * from student"; PreparedStatement pst = con.prepareStatement(sql); ResultSet rst = pst.executeQuery(); while (rst.next()) { int id = rst.getInt(1); String name = rst.getString(2); String time = rst.getString(3); float score = rst.getFloat(4); stu.add(new Student(id, name, time, score)); // System.out.println(id + "---" + name + "---" + age); } closeAll(); return stu; } public void queryById(int id) { try { con = getConn(); String sql = "select name, schoolin, score from student where id=?"; PreparedStatement pst = con.prepareStatement(sql); pst.setInt(1, id); ResultSet rst = pst.executeQuery(); if (rst.next() != false) { String name = rst.getString("name"); String time = rst.getString("schoolin"); float score = rst.getFloat("score"); Student temp = new Student(id, name, time, score); System.out.println(temp); } closeAll(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
TestStudent类
import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class TestStudent { public static void main(String[] args) throws ClassNotFoundException, SQLException { StudentDao st = new StudentDao(); Student s1 = new Student(1, "abc", "2016-12-12", 99); Student s2 = new Student(1, "123", "2010-5-12", 60); int rst = st.addStu(s1); if(rst == 1) { System.out.println("add success!"); } rst = st.addStu(s2); if(rst == 1) { System.out.println("add success!"); } List<Student> stu = new ArrayList<Student>(); stu = st.queryAll(); for(Student st1: stu) { System.out.println(st1.toString()); } String name = "山东理工"; st.updateStu(2, name); List<Student> stu1 = new ArrayList<Student>(); stu = st.queryAll(); for(Student st1: stu) { System.out.println(st1.toString()); } st.queryById(3); } }