学生选课系统

前置条件

创建student表并添加示例数据

功能要求

代码实现
 
  1. import java.sql.*;

  2. import java.util.Scanner;

  3. public class Main {

  4. // 加载驱动

  5. public final static String driver="com.mysql.cj.jdbc.Driver";

  6. public final static String url="jdbc:mysql://localhost:3306/javasql";

  7. /*

  8. 定义了一个公共的、最终的(即不能被重新赋值)、且是静态的字符串变量,名为'url'。

  9. 它的值是"jdbc:mysql://localhost:3306/javasql",

  10. 这通常是一个连接到MySQL数据库的JDBC(Java Database Connectivity)URL。

  11. 这个URL指定了如何连接到位于本地主机(localhost)的MySQL数据库服务器(端口号为3306),

  12. 以及要连接到的数据库名称为'javasql'。

  13. */

  14. public final static String user="root"; // 用户名

  15. public final static String password="654321"; // 登录密码

  16. static Connection conn;

  17. static Scanner in;

  18. static {

  19. try {

  20. // 设置连接对象

  21. Class.forName(driver);

  22. in=new Scanner(System.in);

  23. } catch (ClassNotFoundException e) {

  24. throw new RuntimeException("加载失败");

  25. }

  26. }

  27. // 获取连接对象

  28. public static Connection getConn(){

  29. try{

  30. // 返回连接对象

  31. return DriverManager.getConnection(url,user,password);

  32. } catch (SQLException e) {

  33. e.printStackTrace();

  34. }

  35. return null;

  36. }

  37. // 添加数据

  38. public static void insert() throws SQLException {

  39. System.out.println("\n\n-------插入数据-----------");

  40. String sql="insert into student values(null,?,?,?)";

  41. PreparedStatement ps = conn.prepareStatement(sql);

  42. System.out.print("请输入需要添加的学生个数:");

  43. int cnt=in.nextInt();

  44. while(cnt-- > 0){

  45. System.out.println("\n\n开始添加");

  46. System.out.print("请输入学生姓名:");

  47. ps.setString(1,in.next());

  48. System.out.print("请输入学生电话:");

  49. ps.setString(2,in.next());

  50. System.out.print("请输入学生性别:");

  51. ps.setString(3,in.next());

  52. ps.executeLargeUpdate(); // 执行sql语句

  53. System.out.println("添加成功");

  54. }

  55. ps.close();

  56. return;

  57. }

  58. // 查询数据

  59. public static void inquire() throws SQLException {

  60. System.out.println("\n\n-------查询数据-----------");

  61. System.out.println("1、根据姓名查询 \t\t\t\t 2、根据性别查询");

  62. System.out.println("3、根据电话查询 \t\t\t\t 4、根据电话前缀查询");

  63. System.out.println("5、查询所有数据");

  64. System.out.println("其他输入:退出");

  65. System.out.print("\n请输入你的选择:");

  66. String sql=null;

  67. String choose=in.next();

  68. switch (choose){

  69. case "1" : {

  70. System.out.print("请输入需要查询的学生姓名:");

  71. sql = "select * from student where name = ?"; // sql语句

  72. break;

  73. } case "2" : {

  74. System.out.print("请输入需要查询的性别:");

  75. sql = "select * from student where sex = ?"; // sql语句

  76. break;

  77. } case "3" : {

  78. System.out.print("请输入需要查询的电话号码:");

  79. sql = "select * from student where tel = ?"; // sql语句

  80. break;

  81. } case "4" : {

  82. System.out.print("请输入需要查询的电话号码前缀(注意需要以%为结尾):");

  83. sql = "select * from student where tel like ? "; // sql语句

  84. break;

  85. } case "5" : {

  86. sql="select * from student";

  87. break;

  88. }

  89. default:

  90. System.out.println("退出查询!");

  91. return;

  92. }

  93. PreparedStatement ps=conn.prepareStatement(sql); // 生成预编译sql语句的对象

  94. if(!"5".equals(choose)) ps.setString(1, in.next());

  95. byte flag=0;

  96. ResultSet res = ps.executeQuery();

  97. while(res.next()){

  98. flag=0x01;

  99. System.out.print("学号:"+res.getInt(1)+"\t");

  100. System.out.print("姓名:"+res.getString(2)+"\t");

  101. System.out.print("电话:"+res.getString(3)+"\t");

  102. System.out.println("性别:"+res.getString(4));

  103. }

  104. if(0 == flag){

  105. System.out.println("查无此记录");

  106. }else {

  107. System.out.println("查询成功!");

  108. }

  109. res.close();

  110. ps.close();

  111. return;

  112. }

  113. // 删除数据

  114. public static void delete() throws SQLException{

  115. System.out.println("\n-------删除数据---------");

  116. System.out.println("1、根据id删除 \t\t\t\t 2、根据表中位置删除");

  117. System.out.println("3、根据姓名删除 \t\t\t\t 4、清空所有学生数据");

  118. System.out.println("6、退出操作");

  119. String sql=null;

  120. PreparedStatement ps=null;

  121. System.out.print("请输入你的选择:");

  122. String choose=in.next();

  123. switch (choose){

  124. case "1" : {

  125. System.out.print("请输入需要删除学生的id:");

  126. sql="delete from student where id = ? ";

  127. break;

  128. } case "2" : {

  129. // 连续执行两次语句

  130. System.out.print("请输入需要删除第表中第几条数据:");

  131. sql="set @tmp=(select id from student limit ?,1); ";

  132. ps = conn.prepareStatement(sql);

  133. ps.setInt(1,(in.nextInt()-1));

  134. ps.executeLargeUpdate();

  135. sql="delete from student where id=@tmp ";

  136. ps=conn.prepareStatement(sql);

  137. ps.executeLargeUpdate();

  138. System.out.println("操作成功");

  139. return;

  140. } case "3" : {

  141. System.out.print("请输入需要删除学生的姓名:");

  142. sql="delete from student where name = ? ";

  143. break;

  144. } case "4" : {

  145. sql="delete from student";

  146. } case "6" : {

  147. System.out.println("退出成功!");

  148. return;

  149. }

  150. default:{

  151. System.out.println("请重新选择");

  152. break;

  153. }

  154. }

  155. ps=conn.prepareStatement(sql);

  156. if(!"4".equals(choose)) { ps.setString(1,in.next()); }

  157. ps.executeLargeUpdate(); //执行语句

  158. System.out.println("删除成功");

  159. ps.close();

  160. return;

  161. }

  162. // 修改数据

  163. public static void amend() throws SQLException{

  164. System.out.println("------修改数据--------");

  165. System.out.println("1、修改表中某一位学生的姓名 \t\t\t\t 2、修改表中某一位学生的电话");

  166. System.out.println("其他:退出");

  167. String sql=null;

  168. PreparedStatement ps=null;

  169. System.out.print("\n请输入你的选择:");

  170. String choose=in.next();

  171. if(!("1".equals(choose) || "2".equals(choose))) { System.out.println("退出成功!"); return; }

  172. sql="set @tmp=(select id from student limit ? , 1) ";

  173. System.out.print("请输入修改的第几条数据:");

  174. ps=conn.prepareStatement(sql);

  175. ps.setInt(1,(in.nextInt()-1));

  176. ps.executeLargeUpdate();

  177. switch (choose){

  178. case "1" : {

  179. System.out.print("请输入修改后的姓名:");

  180. sql="update student set name= ? where id=@tmp";

  181. break;

  182. } case "2" : {

  183. System.out.print("请输入修改后的电话:");

  184. sql="update student set tel= ? where id=@tmp";

  185. break;

  186. }

  187. }

  188. ps=conn.prepareStatement(sql);

  189. ps.setString(1,in.next());

  190. ps.executeLargeUpdate();

  191. ps.close();

  192. System.out.println("操作成功!");

  193. return;

  194. }

  195. public static void main(String[] args) throws SQLException {

  196. conn=getConn();

  197. boolean flag=true;

  198. while(flag){

  199. System.out.println("\n----------学生信息管理--------------\n");

  200. System.out.println("1、添加操作 \t\t\t\t 2、查询操作");

  201. System.out.println("3、删除操作 \t\t\t\t 4、修改操作");

  202. System.out.println("5、退出");

  203. System.out.print("\n请输入你的选择:");

  204. switch (in.next()){

  205. case "1" : insert(); break; // 添加

  206. case "2" : inquire();break; // 查询

  207. case "3" : delete();break; // 删除

  208. case "4" : amend();break; // 修改

  209. case "5" : flag=false; break;

  210. default: System.out.println("请重新输入!"); break;

  211. }

  212. }

  213. System.out.println("\n退出成功!");

  214. conn.close();

  215. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值