目录
工具包
学生管理系统包
详细撰写过程请见个人博客https://blog.csdn.net/m0_46383618/article/details/106223960
package com.zzu.tool.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBlink {
private static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");//加载驱动--先复制架包(MySQL数据库驱动程序-mysql-connector-java-5.1.44-bin.jar)
//粘贴到创建的lib里面,然后build pate--add(注意将Package Explorer小箭头里的Package Presentation 变为Hierar)
//在新出现的Libraries里找到jdbc包里面的drive.class,并复制Qulified name得到com.mysql.jdbc.Driver.class
String url="jdbc:mysql://127.0.0.1:3306/test";//指定连接那一台计算机上的那个数据库实例
return DriverManager.getConnection(url, "root", "root");//获取数据库连接的对象,一个对象表示一次数据库的连接
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static boolean exisit(String sql) {//查询
Connection connection = null;//提升一级
Statement statement = null;
ResultSet resultSet = null;
try{
connection = getConnection();
statement = connection.createStatement();//获得Statement对象
resultSet = statement.executeQuery(sql);//执行sql语句,将值赋给resultSet
//此时数据都在resultSet里,需要通过循环将他显示
return resultSet.next();
} catch (Exception e) {
e.printStackTrace();
}finally {//即便有异常也会执行
close(resultSet,statement,connection);
}
return false;
}
public static void select2(String sql,IRowMapper rowMapper) {//接口无法创建对象,所以rowMapper参数一定指向IRowMapper接口实现类
Connection connection = null;//提升一级
Statement statement = null;
ResultSet resultSet = null;
try{
connection = getConnection(); //获取连接
statement = connection.createStatement();//获得Statement对象
resultSet = statement.executeQuery(sql);//执行sql语句,将值赋给resultSet
//此时数据都在resultSet里,需要通过循环将他显示
rowMapper.rowMapper(resultSet);//因为rowMapper参数指向IRrowMapper接口实现类对象,所以此处将调用接口实现类中所实现的rowMapper方法 多态
//如果没有此多态,则main方法里调用时result的值会被finally释放,所以要
} catch (Exception e) {
e.printStackTrace();
}finally {//即便有异常也会执行,释放资源
close(resultSet,statement,connection);
}
}
public boolean date(String sql) {//sql语句,根据sql的不同执行增删改查
Connection connection = null;//提升一级
Statement statement = null;
try {
connection = getConnection(); //获取连接
statement = connection.createStatement();//获得Statement对象
int result = statement.executeUpdate(sql);//执行sql语句,返回受影响的行数,仅限于insert,update,delete
/*statement.close();//如果上面代码出现异常,则该行代码及其下面代码无法执行,所以资源无法释放;比如sql语句语法错误,则statement和connection无法释放
connection.close();*/
return result>0;//处理结果
} catch (Exception e) {
e.printStackTrace();
}finally {//即便有异常也会执行
close(statement,connection);
}
return false;
}
private static void close(Statement statement, Connection connection) {//简化代码,将数据的释放写到一个方法里,直接调用即可
try {
if(statement!=null) {//有可能异常导致statement没有赋值,比如url出错
statement.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if(connection!=null) {
connection.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
private static void close(ResultSet resultSet,Statement statement, Connection connection) {//重载
try {
if(resultSet!=null) {//有可能异常导致statement没有赋值,比如url出错
resultSet.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
close(statement,connection);
}
}
package com.zzu.main;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import com.zzu.tool.db.DBlink;
import com.zzu.tool.db.IRowMapper;
public class Main {
private static DBlink db = new DBlink();
public static void main(String[] args) {
System.out.println("*********************************");
System.out.println("*\t\t\t\t*");
System.out.println("*\t欢迎使用学生信息管理系统\t*");
System.out.println("*\t\t\t\t*");
System.out.println("*********************************");
while (true) {
menu();
}
}
static void menu() {
System.out.println("1、添加学生信息");
System.out.println("2、删除学生信息");
System.out.println("3、修改学生信息");//地址传递
System.out.println("4、查询学生信息");//name
System.out.println("请输入操作,以Enter键结束:");
Scanner scanner = new Scanner(System.in);
int option = scanner.nextInt();
switch (option) {
case 1:{
System.out.println("请输入学号");
String id = scanner.next();
String sql = "select name from student where id = '"+id+"'";
if(db.exisit(sql)) {
System.out.println("学号已存在,操作终止!");
return;
}
System.out.println("请输入姓名");
String name = scanner.next();
System.out.println("请输入手机号");
String mobile = scanner.next();
System.out.println("请输入地址");
String address = scanner.next();
sql="insert into student (id,name,mobile,address) values ('"+id+"','"+name+"','"+mobile+"','"+address+"')";
if(db.date(sql)) {
System.out.println("添加成功!");
}
System.out.println("添加失败!");
break;
}
case 2:{
System.out.println("请输入学号");
String id = scanner.next();
String sql = "select name from student where id = '"+id+"'";
if(!db.exisit(sql)) {
System.out.println("学号不存在,操作终止!");
return;
}
sql="delete from student wherer id='"+id+"'";
if(db.date(sql)){
System.out.println("删除成功!");
}
System.out.println("删除失败!");
break;
}
case 3:{
System.out.println("请输入学号");
String id = scanner.next();
String sql = "select name from student where id = '"+id+"'";
if(!db.exisit(sql)) {
System.out.println("学号不存在,操作终止!");
return;
}
System.out.println("请输入新姓名");
String name = scanner.next();
System.out.println("请输入新手机号");
String mobile = scanner.next();
System.out.println("请输入新地址");
String address = scanner.next();
sql="update student set name='"+name+"','"+mobile+"','"+address+"' where id = '"+id+"'";
if(db.date(sql)){
System.out.println("修改成功!");
return;
}
System.out.println("修改失败!");
break;
}
case 4:{
System.out.println("请输入学号");
String id = scanner.next();
String sql = "select name from student where id = '"+id+"'";
if(!db.exisit(sql)) {
System.out.println("学号不存在,操作终止!");
return;
}
sql="select id,name,mobile,address from student where id = '"+id+"'";
/*class RowMapper implements IRowMapper{
@Override
public void rowMapper(ResultSet rs) {
try {
if(rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
String mobile = rs.getString("mobile");
String address = rs.getString("address");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
db.select2(sql, new RowMapper());*/
db.select2(sql, (ResultSet rs)->{
try {
if(rs.next()) {
String name = rs.getString("name");
String mobile = rs.getString("mobile");
String address = rs.getString("address");
System.out.println(id+","+name+","+mobile+","+address);
}
} catch (SQLException e) {
e.printStackTrace();
}
});
break;
}
default:
System.out.println("I'm Sorry,there is not the "+option+" option,please try again.");
}
}
}