SqlMain import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Scanner;public class Sqlmain {public static void main(String[] args) {Connection con=null;@SuppressWarnings("unused")String drive
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Scanner; public class Sqlmain { public static void main(String[] args) { Connection con=null; @SuppressWarnings("unused") String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL = "jdbc:sqlserver://localhost:1433;databasename=zcxDB"; // 1433是端口,"USCSecondhandMarketDB"是数据库名称 String userName = "sa"; // 用户名 String userPwd = "123456"; // 密 System.out.println("恭喜你连接成功!"); try { con = DriverManager.getConnection(dbURL, userName, userPwd); Scanner sc=new Scanner(System.in); while (true){ System.out.println("+---------请选择将要操作的表--------+"); System.out.println("| 1:学生基本信息表 |"); System.out.println("| 2:成绩表 |"); System.out.println("| 3:学生课程表 |"); System.out.println("+---------4:退出----------------+"); System.out.print("请选择你将要操作的表:"); int input=sc.nextInt(); switch(input){ case 1: new Student(con); break; case 2: new Sc(con); break; case 3: new Course(con); break; case 4: return; } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }学生表
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; public class Student { Statement stmt=null; Scanner sc = new Scanner(System.in); public Student(Connection con){ while(true){ System.out.println("+---------学生基本信息表--------+"); System.out.println("| 1:录入 |"); System.out.println("| 2:修改 |"); System.out.println("| 3:删除 |"); System.out.println("| 4:查询 |"); System.out.println("| 5:退出 |"); System.out.println("+---------------------------+"); System.out.print("请输入选项:"); int input=sc.nextInt(); switch(input){ case 1: scinter(con); break; case 2: scalter(con); break; case 3: scdelete(con); break; case 4: select(con); break; case 5: return; } } } private void select(Connection con){ try { PreparedStatement ps = con.prepareStatement("SELECT * FROM Student"); ResultSet res = ps.executeQuery(); System.out.println( "学号" + " "+ "姓名" + " "+ "性别" + " "+ "生日" + " "+ "专业编号"+" "+ "年级 \n"); while (res.next()) { System.out.println(res.getString(1) + " "+ res.getString(2) + " "+ res.getString(3) + " "+ res.getString(4) + " "+ res.getString(5) + " "+ res.getString(6) + "\n"); } } catch (SQLException e) { System.out.println("你的操作有误!"); } } private void scinter(Connection con){ try { stmt=con.createStatement(); System.out.println("请问需要录入几名同学的信息"); int input=sc.nextInt(); System.out.println("请依次输入:学号 姓名 性别 生日 专业编号 年级"); PreparedStatement sql = con.prepareStatement("insert into sc values(?,?,?,?,?,?)"); for(int i=0;i 成绩表import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; public class Sc { Statement stmt=null; Scanner sc = new Scanner(System.in); public Sc(Connection con) { while(true){ System.out.println("+---------学生成绩表-----------+"); System.out.println("| 1:录入 |"); System.out.println("| 2:修改 |"); System.out.println("| 3:删除 |"); System.out.println("| 4:查询 |"); System.out.println("| 5:退出 |"); System.out.println("+---------------------------+"); System.out.print("请输入选项:"); int input=sc.nextInt(); switch(input){ case 1: scinter(con); break; case 2: scalter(con); break; case 3: scdelete(con); break; case 4: select(con); break; case 5: return; } } } private void select(Connection con) { try { PreparedStatement ps = con.prepareStatement("SELECT * FROM sc"); ResultSet res = ps.executeQuery(); System.out.println( "选课\t编号\t学号\t课程号 "); while (res.next()) { System.out.println(res.getString(1) + "\t" + res.getString(2) + "\t" + res.getString(3) + "\t"+ res.getString(4) ); } } catch (SQLException e) { System.out.println("你的操作有误!"); } } private void scinter(Connection con){ try { stmt=con.createStatement(); System.out.println("请问需要录入几名同学的信息"); int input=sc.nextInt(); System.out.println("请依次输入:选课编号 学号 课程号 成绩"); PreparedStatement sql = con.prepareStatement("insert into sc values(?,?,?,?)"); for(int i=0;i 课程表import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class Course { Scanner sc=new Scanner(System.in); public Course(Connection con){ while(true){ System.out.println("+---------课程信息表-----------+"); System.out.println("| 1:录入 |"); System.out.println("| 2:修改 |"); System.out.println("| 3:删除 |"); System.out.println("| 4:查询 |"); System.out.println("| 5:退出 |"); System.out.println("+---------------------------+"); System.out.print("请输入选项:"); int input=sc.nextInt(); switch(input){ case 1:cinser(con); break; case 4:cselect(con); break; } } } public void cinser(Connection con){ try { System.out.println("请问需要录入几名同学的信息"); int input=sc.nextInt(); System.out.println("请依次输入:课程号 先选课程号 课程名称 开设年级 学分"); PreparedStatement sql = con.prepareStatement("insert into sc values(?,?,?,?,?)"); for(int i=0;i