新生管理系统,加入了数据库后永久保存(只是学生的增删改查方法)

package com.wmgj.java.packages018.mysql;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.Scanner;import com.wmgj.java.packages018.newmysql.Students;public class Demo {Scanner sc = new Scanner(System.in);String driver = "com.mysql.jdbc.Driver";String url = "jdbc:mysql://localhost:3306/manage?characterEncoding=UTF-8";String user = "root";String password = "a123";Statement sql;boolean temp ;Connection con;ResultSet st = null;private static Demo demo = null;String name;/** * 设计模式:单例模式。即使多次调用并进行实例化,也只会被实例化一次 */public static Demo getInstance(){if(demo ==null){demo = new Demo();}return demo;}/** * 加载驱动、连接数据库 */public void connectSql() {try {Class.forName(driver);System.out.println("加载特定驱动");/** 连接数据库 */con = DriverManager.getConnection(url, user, password);sql = con.createStatement();System.out.println("连接数据库完成");} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}}/** * 添加数据 */public void addToSql() {System.out.println("--------进入学生信息添加系统-----------");System.out.println("请输入姓名 :");String s1 = sc.next();System.out.println("请输入性别 :");String s2 = sc.next();System.out.println("请输入地址 :");String s3 = sc.next();System.out.println("请输入分数 :");String s4 = sc.next();try {temp = sql.execute("insert into students (name,sex,address,score) values('" + s1+ "','" + s2 + "','" + s3 + "','" + s4 + "')");System.out.println(temp ? "添加失败" : "添加成功");System.out.println("--------退出添加学生信息系统-----------");System.out.println();} catch (SQLException e) {e.printStackTrace();}}/** * 修改数据 */public void select() {System.out.println("请按提示操作:1:修改id 2:修改姓名 3:修改性别 4:修改地址 5:修改分数");int s = sc.nextInt();switch (s) {case 1:revampId();break;case 2:revampName();break;case 3:revampSex();break;case 4:revampAddress();break;case 5:revampScore();break;default:System.out.println("请重新选择");select();}}/** 修改id */public void revampId() {System.out.println("请输入想要修改的新的id :");int n = sc.nextInt();System.out.println("请输入原来的id地址 :");int k = sc.nextInt();try {temp = sql.execute("update students set id = '" + n+ "' where id='" + k + "'");System.out.println(temp ? "修改失败" : "修改成功");} catch (SQLException e) {e.printStackTrace();}}/** 修改姓名 */public void revampName() {System.out.println("请输入将要替换的姓名 :");String n = sc.next();System.out.println("请输入修改次姓名的id地址 :");String k = sc.next();try {temp = sql.execute("update students set name ='" + n+ "' where id='" + k + "'");System.out.println(temp ? "修改失败" : "修改成功");} catch (SQLException e) {e.printStackTrace();}}/** 修改性别 */public void revampSex() {System.out.println("请输入想要修改的性别 :");String n = sc.next();System.out.println("请输入修改次姓别的id地址 :");String k = sc.next();try {temp = sql.execute("update students set sex = '" + n+ "' where id='" + k + "'");System.out.println(temp ? "修改失败" : "修改成功");} catch (SQLException e) {e.printStackTrace();}}/** 修改地址 */public void revampAddress() {System.out.println("请输入新的地址");String n = sc.next();System.out.println("请输入修改次地址的id地址 :");String k = sc.next();try {temp = sql.execute("update students set address = '" + n+ "' where id='" + k + "'");System.out.println(temp ? "修改失败" : "修改成功");} catch (SQLException e) {e.printStackTrace();}}/** 修改分数 */public void revampScore() {System.out.println("请输入新的分数");String n = sc.next();System.out.println("请输入修改次分数的id地址 :");String k = sc.next();try {temp = sql.execute("update students set score = '" + n+ "' where id='" + k + "'");System.out.println(temp ? "修改失败" : "修改成功");} catch (SQLException e) {e.printStackTrace();}}/** *列出所有的数据 */public void showAllOfSql() {try {st = sql.executeQuery("select * from students");whileToSql();System.out.println();System.out.println("--------退出列表系统-----------");System.out.println();} catch (SQLException e) {e.printStackTrace();} finally {try {if (st != null) {st.close();}} catch (SQLException e) {e.printStackTrace();}}}/** * 按主键:id 查询相关信息 */public void demandTOSql() {try {System.out.println("--------进入查询系统-----------");System.out.println("请输入id查询:");int n = sc.nextInt();st = sql.executeQuery("select * from students ");boolean flag = true;while(st.next()){int id = st.getInt("id");if(id == n){System.out.println("查询结果如下:");System.out.println("id" + "\t" + "name" + "\t" + "sex" + "\t"+ "address" + "\t" + "score");replace(id);System.out.println("--------退出查询系统-----------");System.out.println();flag = false;break;}}if(flag){System.out.println("没有找到该id的学生信息!");System.out.println("--------退出查询系统-----------");System.out.println();}} catch (SQLException e) {e.printStackTrace();}}/** * 遍历取数据 */public void replace(int id){try {String name = st.getString("name");String sex = st.getString("sex");String address = st.getString("address");String score = st.getString("score");System.out.println(id + "\t" + name + "\t" + sex + "\t"+ address + "\t" + score);} catch (SQLException e) {e.printStackTrace();}}/** * 循环输出 */public void whileToSql() {try {System.out.println("全部学生信息如下:");System.out.println("id" + "\t" + "name" + "\t" + "sex" + "\t"+ "address" + "\t" + "score");while (st.next()) {int id = st.getInt("id");replace(id);}} catch (SQLException e) {e.printStackTrace();}}/** * 输入id来删除这一行 */public void deleteToSql() {try {System.out.println("--------进入删除系统-----------");System.out.println("请输入id来删除学生信息: ");int n = sc.nextInt();st = sql.executeQuery("select * from students");boolean flag = true;while (st.next()) {int id = st.getInt("id");if (id == n) {boolean s = sql.execute("delete from students where id='"+ id + "' ");System.out.println(s ? "删除失败" : "删除成功");System.out.println("--------退出删除系统-----------");System.out.println();flag = false;break;}}if(flag){System.out.println("没有此id,请重新确认!");System.out.println("--------退出删除系统-----------");System.out.println();}} catch (SQLException e) {e.printStackTrace();}}/** * 关闭数据库、退出 */public void close() {try {sql.close();con.close();} catch (SQLException e) {e.printStackTrace();}}/** * 主操作程序入口 */public void interfaceTochoice() {System.out.println("请按提示操作:1:列出所有学生信息;2:添加信息;3:删除学生;4:用id查询信息;5:修改信息;6:退出");int a = sc.nextInt();switch (a) {case 1:showAllOfSql();interfaceTochoice();break;// 列表所有case 2:addToSql();interfaceTochoice();break;// 添加(id,name,sex,address,score)056,"甘肃","版本","数据","9+1"case 3:deleteToSql();interfaceTochoice();break;// 删除case 4:demandTOSql();interfaceTochoice();break;// 查询case 5:select();interfaceTochoice();break;// 按提示修改case 6:close();break;// 退出default:interfaceTochoice();}}public static void main(String[] args) {Demo demo = new Demo();demo.connectSql();demo.interfaceTochoice();}}想让输入数据库里面的表的名字的,但是好像不能实现,就没有改了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值