昨天的作业内容:
package mysqljdbc; import java.math.BigDecimal; import java.sql.*; import java.util.Scanner; public class jdbcmysqljoin { static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("有如下操作给您选择!!!"); System.out.println("1:查询学生信息"); System.out.println("2:添加学生信息"); System.out.println("3:删除学生信息"); System.out.println("4:修改学生信息"); System.out.println("请输入你要选择操作的序号"); int stop=scan.nextInt(); switch (stop){ case 1:select();students();students(); break; case 2:insert();select();students();break; case 3:delete();select();students();break; case 4:update();select();students();break; default: System.out.println("退出成功");; } /* if(stop==1){ select(); }else if(stop==2){ insert(); return; }else if (stop==3){ delete(); return; }else if(stop==4){ update(); return; }else{ System.out.println("请选择你要进行的操作"); System.out.println("1:查询学生信息"); System.out.println("2:添加学生信息"); System.out.println("3:删除学生信息"); System.out.println("4:修改学生信息"); } //delete();//删除 // insert();//添加 // update();//修改 //select();//查询*/ } //连接数据库查询数据 private static void select(){ try { Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student2?useUnicode=true&characterEncoding=UTF-8","root",""); System.out.println("数据库连接成功"); PreparedStatement statement=conn.prepareStatement("select * from student"); ResultSet result=statement.executeQuery(); System.out.println("学生信息如下"); while (result.next()){ String sno=result.getString("sno"); String sname=result.getString("sname"); String ssex=result.getString("ssex"); Date sbirthday=result.getDate("sbirthday"); String classno=result.getString("class"); System.out.println("学号: "+sno+" 姓名: "+sname+" 性别: "+ssex+" 出生日期: "+sbirthday+" 编号: "+classno); } result.close(); conn.close(); statement.close(); } catch (SQLException e) { e.printStackTrace(); } } //添加学生数据 private static void insert(){ try { Scanner scanner=new Scanner(System.in); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student2?useUnicode=true&characterEncoding=UTF-8","root",""); PreparedStatement statement=conn.prepareStatement("insert into student values (?,?,?,?,?)"); System.out.println("学号: "); String sno=scanner.nextLine(); System.out.println("姓名: "); String sname=scanner.nextLine(); System.out.println("性别: "); String ssex=scanner.nextLine(); System.out.println("出生日期: "); String sbirthday=scanner.nextLine(); System.out.println("编号: "); String classno=scanner.nextLine(); statement.setString(1,sno); statement.setString(2,sname); statement.setString(3,ssex); statement.setString(4,sbirthday); statement.setString(5,classno); int rows=statement.executeUpdate(); if (rows>0){ System.out.println("添加成功"); }else{ System.out.println("添加失败"); } statement.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } //删除学生信息 private static void delete(){ Scanner scanner=new Scanner(System.in); try { Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student2?useUnicode=true&characterEncoding=UTF-8","root",""); PreparedStatement statement=conn.prepareStatement("delete from student where sno=?"); System.out.println("请输入你要删除的学生学号"); String sno=scanner.nextLine(); statement.setString(1,sno); int rows=statement.executeUpdate(); if(rows>0){ System.out.println("删除成功"); }else{ System.out.println("删除失败"); } statement.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } //修改学生信息 private static void update(){ try { Scanner scanner=new Scanner(System.in); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student2?useUnicode=true&characterEncoding=UTF-8","root",""); PreparedStatement statement=conn.prepareStatement("update student set sname=? where sno=? "); System.out.println("请输入你要修改的学生的学号:"); String sno=scanner.nextLine(); System.out.println("请输入你将要修改学生最后的姓名:"); String sname=scanner.nextLine(); statement.setString(1,sname); statement.setString(2,sno); int rows=statement.executeUpdate(); if(rows>0){ System.out.println("修改成功"); }else{ System.out.println("修改失败"); } statement.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void students(){ Scanner scan=new Scanner(System.in); System.out.println("有如下操作给您选择!!!"); System.out.println("1:查询学生信息"); System.out.println("2:添加学生信息"); System.out.println("3:删除学生信息"); System.out.println("4:修改学生信息"); System.out.println("请输入你要选择操作的序号"); int stop=scan.nextInt(); switch (stop){ case 1:select();students();break; case 2:insert();select();break; case 3:delete();select();break; case 4:update();select();break; default:select(); } students(); } }