用jdbc对mysql进行简单的操作——学生系统

复制的时候请改一下包名
今天听了周老师讲了jdbc的功能之后,随手写的一个小系统
Date:2019/10/11 15:27
writer:应宗道
耗时:0.5h

package studentOperation;

import java.sql.*;
import java.util.Scanner;

public class Begin {
static Statement statement = null;//由Connection产生,负责执行sql语句
static Scanner sc = new Scanner(System.in);
static String bridge = “jdbc:mysql://localhost:3306/test”;//建立与数据库的连接
static String userName = “root”;//用户名
static String password = “root”;//密码

public static void main(String[] args) {
    connectToSql();
}

public static void connectToSql() {//连接到数据库
    confirmDrive();
    createBridge();
}

public static void confirmDrive() {//确定驱动
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

public static void createBridge() {//建立桥梁
    try {
        Connection connection = DriverManager.getConnection(bridge, userName, password);
        statement = connection.createStatement();
        caidan();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

public static void caidan() {//菜单
    boolean bool = true;
    while (bool) {
        System.out.println("1.add    2.query    3.update    4.delete");
        String choose = sc.next();
        int result = judgeIsInt(choose);

// System.out.println(result);
if (result == 1) {
add();
} else if (result == 2) {
query();
} else if (result == 3) {
update();
} else if (result == 4) {
delete();
} else {
bool = false;
System.out.println(“退出成功!”);
}
}
}

public static int judgeIsInt(String choose) {
    int integer = -1;
    try {
        integer = Integer.parseInt(choose);
    } catch (Exception e) {
        System.out.println("输入非法数字");
    }
    return integer;
}

public static void add() {
    System.out.println("请输入要添加的学生学号:");
    String number = sc.next();
    int result = judgeIsInt(number);
    if (result != -1) {
        System.out.println("请输入要添加的学生姓名:");
        String name = sc.next();
        try {
            statement.execute("insert into stu values(" + number + "," + name + ")");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println("添加成功!");
    }
}

public static void query() {
    ResultSet resultSet = null;
    try {
        resultSet = statement.executeQuery("select * from stu");
        while (resultSet.next()) {
            System.out.println("学号:" + resultSet.getInt("id") + "\t姓名:" + resultSet.getString("name"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

public static Object[] updateBeforeFind() {//修改前查找
    boolean bool = false;
    System.out.println("请输入要修改的学生学号:");
    String number = sc.next();
    int result = judgeIsInt(number);
    try {
        ResultSet resultSet = statement.executeQuery("select * from stu where id =" + number);
        while(resultSet.next()){
            if (resultSet.getString("id").equals(number)){
                bool = true;
            }
        }
        resultSet.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    Object[] object = {bool,number};
    return object;
}

public static void update() {
    Object[] object = updateBeforeFind();
    if ((boolean)object[0]){
        System.out.println("请输入新的学生姓名:");
        String newName = sc.next();
        try {
            statement.executeUpdate("update stu set name ='"+newName+"'where id ='"+object[1]+"'");
            System.out.println("修改成功!");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

public static Object[] deleteBeforeFind() {//删除前查找
    boolean bool = false;
    System.out.println("请输入要删除的学生学号:");
    String number = sc.next();
    int result = judgeIsInt(number);
    try {
        ResultSet resultSet = statement.executeQuery("select * from stu where id =" + number);
        while(resultSet.next()){
            if (resultSet.getString("id").equals(number)){
                bool = true;
            }
        }
        resultSet.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    Object[] object = {bool,number};
    return object;
}

public static void delete() {
    Object[] object = deleteBeforeFind();
    if ((boolean)object[0]){
        try {
            statement.executeUpdate("delete from stu where id ='"+object[1]+"'");
            System.out.println("删除成功!");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值