package aboutdb1; import java.sql.*; import java.util.Scanner; public class newDBsystem { private static Connection getConnection() throws Exception { Class.forName("com.mysql.cj.jdbc.Driver"); // 加载MySQL JDBC驱动 Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db1?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false", "root", "123456");// 使用DriverManager获取数据库连接 return con; } static Scanner sc=new Scanner(System.in); //main主类控制执行顺序 public static void main(String[] args)throws Exception { while (true){ System.out.println("====数仓管理系统====="); System.out.println("1.增 2.删 3.改 4.查 5.退出 "); int number = sc.nextInt(); if (number==1){ addmodel(); } if (number==2){ delmodel(); } if (number==3){ updatamodel(); } if (number==4){ checkmodel(); } if (number==5){ break; } } } //添加功能 public static void addmodel() throws Exception { Connection con =getConnection(); System.out.println("===插入界面==="); System.out.print("id:"); int id = sc.nextInt(); System.out.print("name:"); String name =sc.next(); System.out.print("count:"); int count = sc.nextInt(); System.out.print("price:"); int price = sc.nextInt(); PreparedStatement ps =con.prepareStatement("insert into shoping (id,name,count,price) values(?,?,?,?)"); // 创建PreparedStatement对象 ps.setInt(1,id); ps.setString(2,name); ps.setInt(3,count); ps.setInt(4,price); ps.execute(); System.out.println("插入成功"); } //查看功能 public static void checkmodel() throws Exception { Connection con =getConnection(); String sql = "SELECT * FROM shoping"; PreparedStatement ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); System.out.println("================================="); while (rs.next()) { // 遍历结果集 // 使用rs.getXXX("columnName")方法获取每一列的值,并打印出来 System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", count: " + rs.getInt("count") + ", price: " + rs.getInt("price")); } ps.execute(); System.out.println("================================="); } //删除功能 public static void updatamodel() throws Exception { Connection con =getConnection(); System.out.println("===修改界面==="); infoidname(); System.out.print("请输入所要删除的数据对应id,以及其他字段想要修改的值:"); System.out.print("id:"); int id = sc.nextInt(); System.out.print("其他字段的值:"); System.out.print("name:"); String name =sc.next(); System.out.print("count:"); int count = sc.nextInt(); System.out.print("price:"); int price = sc.nextInt(); PreparedStatement ps =con.prepareStatement("update shoping set name =?,count =?,price=? where id=?"); ps.setString(1,name); ps.setInt(2,count); ps.setInt(3,price); ps.setInt(4,id); int i=ps.executeUpdate(); if (i>0){ System.out.println("修改成功!"); } } //修改功能 public static void delmodel() throws Exception { Connection con =getConnection(); System.out.println("===删除界面==="); System.out.print("请输入所要删除的数据对应id:"); int id = sc.nextInt(); PreparedStatement ps =con.prepareStatement("delete from shoping where id = ?"); ps.setInt(1,id); ps.execute(); System.out.println("删除成功"); } //查看id及name public static void infoidname() throws Exception { Connection con = getConnection(); System.out.println("id及名称列表:"); PreparedStatement ps=con.prepareStatement("select id from shoping"); ResultSet resultSet = ps.executeQuery(); while (resultSet.next()){ System.out.print("id:"+resultSet.getInt("id")); System.out.print(" "); } System.out.println(); PreparedStatement ps1=con.prepareStatement("select name from shoping"); ResultSet resultSet1 = ps1.executeQuery(); while (resultSet1.next()){ System.out.print("name:"+resultSet1.getString("name")); System.out.print(" "); } System.out.println(); } }
Java学习Day9之数据库链接java
于 2024-07-19 15:36:30 首次发布