学生信息管理系统(JDBC使用完成)

    技术2023-10-13  65

    使用jdbc来完成简单的学生信息管理系统

    项目框架

    Main.java

    package com.zzt.Student.client; import java.sql.ResultSet; import java.util.Scanner; import java.util.UUID; import com.zzt.Student.tool.db.DBLink; import com.zzt.Student.tool.db.IRowMapper; public class Main { public static void main(String[] args) { System.out.println("*********************************"); System.out.println("*\t\t\t\t*"); System.out.println("*\t欢迎使用学生信息管理系统\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、查询学生信息"); 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 id from student where id=?"; if(new DBLink().exist(sql, id)) {//查询用户名是否已经存在 System.out.println("学号存在,添加操作终止"); return; } System.out.println("请输入姓名:"); String name = scanner.next(); System.out.println("请输入地址:"); String address = scanner.next(); System.out.println("请输入手机号:"); String mobile = scanner.next(); sql = "insert into student (id,name,address,mobile) values (?,?,?,?)"; if(new DBLink().update(sql, id,name,address,mobile)) { System.out.println("添加成功"); return; } System.out.println("添加失败"); break; } case 2:{ System.out.println("请输入学号:"); String id = scanner.next(); String sql= "select id from student where id=?"; if(!new DBLink().exist(sql, id)) { System.out.println("学号不存在,删除操作终止"); return; } sql= "delete from student where id=?"; if(new DBLink().update(sql,id)) { System.out.println("删除成功"); return; } System.out.println("删除失败"); break; } case 3:{ System.out.println("请输入学号:"); String id = scanner.next(); String sql= "select id from student where id=?"; if(!new DBLink().exist(sql, id)) { System.out.println("学号不存在,修改操作终止"); return; } System.out.println("请输入新的姓名:"); String name = scanner.next(); System.out.println("请输入新的地址:"); String address = scanner.next(); System.out.println("请输入新的手机号:"); String mobile = scanner.next(); sql="update student set name=?,address=?,mobile=? where id=?"; if(new DBLink().update(sql,name,address,mobile,id)) { System.out.println("修改成功"); return; } System.out.println("修改失败"); break; } case 4:{ System.out.println("请输入学号"); String id =scanner.next(); String sql="select id,name,address,mobile from student where id=? "; DBLink db = new DBLink(); db.select(sql,(rs)->{//此处是lambda表达式(1.8版本后使用) try { if(rs.next()) { String name = rs.getString("name"); String address = rs.getString("address"); String mobile = rs.getString("mobile"); System.out.println(id+","+name+","+address+","+mobile); return; } System.out.println("没有找到学生信息"); } catch (Exception e) { e.printStackTrace(); } },id); break; } default: System.out.println("I'm Sorry,there is not the "+option+" option,please try again."); } } }

    DBLink.java

    package com.zzt.Student.tool.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.apache.log4j.Logger; import com.zzt.Student.tool.PropertiesTool; public class DBLink { private Logger logger = Logger.getLogger(DBLink.class); /** * 获取数据库连接 * *@author Dell */ private Connection GetConnection() { try { Class.forName("com.mysql.jdbc.Driver");//加载驱动 String username = PropertiesTool.getValue("db.username"); String password = PropertiesTool.getValue("db.password"); String url =PropertiesTool.getValue("db.url"); return DriverManager.getConnection(url, "root", "root");//获取连接 } catch (Exception e) { logger.debug(e.getMessage(),e); } return null; } /** * 判断数据库能否查出数据 * *@author Dell */ public boolean exist(String sql) { Connection connection = null; Statement statement =null; ResultSet resultSet=null; try { connection = GetConnection();//获取连接 statement = (Statement) connection.createStatement(); resultSet= ((java.sql.Statement) statement).executeQuery(sql);//执行sql,将查询的数据存到ResultSet类型的变量中 return resultSet.next(); }catch (Exception e) { logger.debug(e.getMessage(),e); }finally { close(resultSet,statement,connection); } return false; } /** * 判断数据库能否查出数据 * *@author Dell */ public boolean exist(String sql,Object ...params) { Connection connection = null; PreparedStatement preparedstatement =null; ResultSet resultSet=null; try { connection = GetConnection();//获取连接 preparedstatement = connection.prepareStatement(sql); for (int i = 0; i < params.length; i++) { preparedstatement.setObject(i+1,params[i]); } resultSet= preparedstatement.executeQuery();//执行sql,将查询的数据存到ResultSet类型的变量中 return resultSet.next(); }catch (Exception e) { logger.debug(e.getMessage(),e); }finally { close(resultSet,preparedstatement,connection); } return false; } /** * 查询数据 * *@author Dell */ public void select(String sql,IRowMapper rowMapper) { Connection connection = null; Statement statement =null; ResultSet resultSet=null; try { connection = GetConnection();//获取连接 statement = connection.createStatement(); resultSet= statement.executeQuery(sql);//执行sql,将查询的数据存到ResultSet类型的变量中 rowMapper.rowMapper(resultSet); }catch (Exception e) { logger.debug(e.getMessage(),e); }finally { close(resultSet,statement,connection); } } /** * 查询数据 * *@author Dell */ public void select(String sql,IRowMapper rowMapper,Object ...params) { Connection connection = null; PreparedStatement preparedstatement =null; ResultSet resultSet=null; try { connection = GetConnection();//获取连接 preparedstatement = connection.prepareStatement(sql); for (int i = 0; i < params.length; i++) { preparedstatement.setObject(i+1,params[i]); } resultSet= preparedstatement.executeQuery(); rowMapper.rowMapper(resultSet); }catch (Exception e) { logger.debug(e.getMessage(),e); }finally { close(resultSet,preparedstatement,connection); } } /** * 修改(delete,insert,和update)数据 * *@author Dell */ public boolean update(String sql) { Connection connection = null; Statement s = null; try { connection = GetConnection(); System.out.println(connection); s = connection.createStatement(); int result = s.executeUpdate(sql); return result>0; } catch (Exception e) { logger.debug(e.getMessage(),e); }finally { close(s,connection); } return false; } /** * 修改(delete,insert,和update)数据 * *@author Dell */ public boolean update(String sql,Object ...params) { Connection connection = null; PreparedStatement preparedStatement = null; try { connection = GetConnection(); preparedStatement = connection.prepareStatement(sql); for (int i = 0; i < params.length; i++) { preparedStatement.setObject(i+1,params[i]); } return preparedStatement.executeUpdate()>0; } catch (Exception e) { e.printStackTrace(); }finally { close(preparedStatement,connection); } return false; } /** * 释放资源 * *@author Dell */ private void close(Statement statement,Connection connection) { try { if(statement!=null) { statement.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if(connection!=null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } /** * 释放资源 * *@author Dell */ private void close(ResultSet resultSet,java.sql.Statement statement,Connection connection) { try { if(resultSet!=null) { resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); } close(statement,connection); } }

    IRowMapper.java

    package com.zzt.Student.tool.db; import java.sql.ResultSet; public interface IRowMapper { void rowMapper(ResultSet rs); }

    PropertiesTool.java

    package com.zzt.Student.tool; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertiesTool { private static Properties properties = new Properties(); static { InputStream inputStream = PropertiesTool.class.getClassLoader().getResourceAsStream("db.properties");//将db.properties变为javaIO流对象 try { properties.load(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static void main(String [] ages) { String userName = properties.getProperty("db.username"); System.out.print(userName); } public static String getValue(String key) { String value = properties.getProperty(key); return value; } }

    db.properties

    db.username=root db.password=root db.url=jdbc:mysql://localhost:3306/test

    log4j.properties

    log4j.rootLogger=DEBUG,Console,RollingFile #\u5C06\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u63A7\u5236\u53F0 log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern= [%-5p]-[%d{yyyy-MM-dd HH:mm:ss}] -%l -%m%n #\u5C06\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u64CD\u4F5C\u7CFB\u7EDFD\u76D8\u6839\u76EE\u5F55\u4E0B\u7684log.log\u6587\u4EF6\u4E2D log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender log4j.appender.RollingFile.File=D://log.log log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout log4j.appender.RollingFile.layout.ConversionPattern=%d [%t] %-5p %-40.40c %X{traceId}-%m%n \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014 \u7248\u6743\u58F0\u660E\uFF1A\u672C\u6587\u4E3A\u535A\u4E3B\u300CLinDadaxia\u300D\u7684\u539F\u521B\u6587\u7AE0\uFF0C\u9075\u5FAACC 4.0 BY-SA\u7248\u6743\u534F\u8BAE\uFF0C\u8F6C\u8F7D\u8BF7\u9644\u4E0A\u539F\u6587\u51FA\u5904\u94FE\u63A5\u53CA\u672C\u58F0\u660E\u3002 \u539F\u6587\u94FE\u63A5\uFF1Ahttps://blog.csdn.net/LinDadaxia/article/details/107080414

     

    Processed: 0.015, SQL: 9