The Java connection to the mysql database uses the mysql-JDBC jar package, and the executeQuery() method is used for the query operation.
Code:
Student class
package test; public class Student { private int id; private String name; private int age; public Student(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
Sql class:
package test; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Sql { Connection con; public Connection getConn() throws SQLException, ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8", "root", "helloworld"); System.out.println(con); return con; } public int addStu(Student st) throws SQLException, ClassNotFoundException { con=getConn(); try { con = getConn(); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace (); } String sql_insert = "insert into student(id, name, age) values(?, ?, ?)"; PreparedStatement pst = con.prepareStatement(sql_insert); pst.setInt(1, st.getId()); pst.setString(2, st.getName()); pst.setInt(3, st.getAge()); int rest = pst.executeUpdate(); pst.close(); con.close(); return rest; } 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(); pst.close(); con.close(); return result; } public int updateStu(int id, int flag) throws SQLException, ClassNotFoundException { con=getConn(); Scanner cin = new Scanner(System.in); String name; int age; String sql; int rst = 0; if(flag == 1) { name = cin.next(); sql = "update student set name = ? where id = ?"; PreparedStatement pst = con.prepareStatement(sql); pst.setString(1, name); pst.setInt(2, id); rst = pst.executeUpdate(); pst.close(); } else if(flag == 2) { age = cin.nextInt(); sql = "update student set age = ? where id = ?"; PreparedStatement pst = con.prepareStatement(sql); pst.setInt(1, age); pst.setInt(2, id); rst = pst.executeUpdate(); pst.close(); } else if(flag == 3) { age = cin.nextInt(); name = cin.next(); sql = "update student set name = ?, age = ? where id = ?"; PreparedStatement pst = con.prepareStatement(sql); pst.setString(1, name); pst.setInt(2, age); pst.setInt(3, id); rst = pst.executeUpdate(); pst.close(); } con.close(); 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); int age = rst.getInt(3); stu.add(new Student(id, name, age)); //System.out.println(id + "---" + name + "---" + age); } pst.close(); con.close(); return stu; } public void queryById(int id) throws SQLException, ClassNotFoundException { con=getConn(); String sql = "select name, age 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"); int age = rst.getInt("age"); System.out.println("Student number "+id+": "+"\n"+"Name: "+name+"\n"+"Age: "+age+"\n"); } pst.close(); con.close(); } }
Main class;
package test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) throws ClassNotFoundException, SQLException { Scanner cin = new Scanner(System.in); Sql mysql_obj = new Sql(); int order = 0; System.out.println("Please enter the command to operate: "); System.out.println("Enter 1 to add student information"); System.out.println("Enter 2 to delete student information"); System.out.println("Enter 3 to modify student information"); System.out.println("Enter 4 to query according to student ID"); System.out.println("Enter 5 to query all student information"); while(cin.hasNext()) { order = cin.nextInt(); if(order == 1) { System.out.println("Enter student information id name age"); Student stu = new Student(cin.nextInt(), cin.next(), cin.nextInt()); int rst = mysql_obj.addStu(stu); if(rst == 1) { System.out.println("success!"); } else { System.out.println("failed!"); } } else if(order == 2) { System.out.println("Enter student id"); int id = cin.nextInt (); int rst = mysql_obj.deleteStu(id); if(rst == 1) { System.out.println("success!"); } else { System.out.println("failed!"); } } else if(order == 3) { System.out.println("Enter student id"); int id = cin.nextInt (); System.out.println("Change Name Input 1 Change Age Input 2 Change both Input 3"); int flag = cin.nextInt(); int rst = mysql_obj.updateStu(id, flag); if(rst == 1) { System.out.println("success!"); } else { System.out.println("failed!"); } } else if(order == 4) { System.out.println("Enter student id"); int id = cin.nextInt (); mysql_obj.queryById(id); } else if(order == 5) { List<Student> stu = new ArrayList<Student>(); stu = mysql_obj.queryAll(); for(Student st: stu) { System.out.println(st.toString()); } } } } }