From 1007ca363a67f3dbc127ad4b46a326f8777934dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=81=E4=B8=9A=E6=9E=97-17050417?= <17050417@suning-sports.com> Date: Sun, 25 Sep 2022 20:36:31 +0800 Subject: [PATCH] =?UTF-8?q?8=E3=80=819=201.=E5=88=A0=E9=99=A4=E3=80=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=202.=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/msb/test1/TestJDBC4.java | 87 +++++++++++++++++++ .../src/com/msb/test1/TestJDBC5.java | 63 ++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 testJDBC/JDBCDemo1/src/com/msb/test1/TestJDBC4.java create mode 100644 testJDBC/JDBCDemo1/src/com/msb/test1/TestJDBC5.java diff --git a/testJDBC/JDBCDemo1/src/com/msb/test1/TestJDBC4.java b/testJDBC/JDBCDemo1/src/com/msb/test1/TestJDBC4.java new file mode 100644 index 0000000..630c464 --- /dev/null +++ b/testJDBC/JDBCDemo1/src/com/msb/test1/TestJDBC4.java @@ -0,0 +1,87 @@ +package com.msb.test1; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; + +public class TestJDBC4 { + public static String driver = "com.mysql.cj.jdbc.Driver"; + public static String url = "jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; + public static String user = "root"; + public static String password = "Fnst1234"; + + + public static void main(String[] args) { +// testDelete(); + testUpdate(); + } + + public static void testUpdate() { + Connection connection = null; + Statement statement = null; + + try { + // 选择数据库 +// Driver driver = new com.mysql.cj.jdbc.Driver(); +// DriverManager.registerDriver(driver); + Class.forName(driver); + + // 建立连接 + connection = DriverManager.getConnection(url, user, password); + + // 执行sql + statement = connection.createStatement(); + String sql = "update dept set dname='总部',loc='北京' where deptno=51"; + int rows = statement.executeUpdate(sql); + System.out.println("影响行数据:" + rows); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (statement != null) { + statement.close(); + } + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + public static void testDelete() { + Connection connection = null; + Statement statement = null; + + try { + // 选择数据库 +// Driver driver = new com.mysql.cj.jdbc.Driver(); +// DriverManager.registerDriver(driver); + Class.forName(driver); + + // 建立连接 + connection = DriverManager.getConnection(url, user, password); + + // 执行sql + statement = connection.createStatement(); + String sql = "delete from dept where deptno=40;"; + int rows = statement.executeUpdate(sql); + System.out.println("影响行数据:" + rows); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (statement != null) { + statement.close(); + } + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + } +} diff --git a/testJDBC/JDBCDemo1/src/com/msb/test1/TestJDBC5.java b/testJDBC/JDBCDemo1/src/com/msb/test1/TestJDBC5.java new file mode 100644 index 0000000..3e28e87 --- /dev/null +++ b/testJDBC/JDBCDemo1/src/com/msb/test1/TestJDBC5.java @@ -0,0 +1,63 @@ +package com.msb.test1; + +import java.sql.*; + +public class TestJDBC5 { + public static String driver = "com.mysql.cj.jdbc.Driver"; + public static String url = "jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; + public static String user = "root"; + public static String password = "Fnst1234"; + + + public static void main(String[] args) { + testQuery(); + } + + public static void testQuery() { + Connection connection = null; + Statement statement = null; + ResultSet resultSet = null; + + try { + // 选择数据库 +// Driver driver = new com.mysql.cj.jdbc.Driver(); +// DriverManager.registerDriver(driver); + Class.forName(driver); + + // 建立连接 + connection = DriverManager.getConnection(url, user, password); + + // 执行sql + statement = connection.createStatement(); + String sql = "select * from emp;"; + resultSet = statement.executeQuery(sql); + while (resultSet.next()) { + int empno = resultSet.getInt("empno"); + String ename = resultSet.getString("ename"); + String job = resultSet.getString("job"); + int mgr = resultSet.getInt("mgr"); + Date hiredate = resultSet.getDate("hiredate"); + double sal = resultSet.getDouble("sal"); + double comm = resultSet.getDouble("comm"); + int deptno = resultSet.getInt("deptno"); + System.out.println("" + empno + "\t" + ename + "\t" + job + "\t" + mgr + "\t" + hiredate + "\t" + sal + "\t" + comm + "\t" + deptno + "\t"); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (resultSet != null) { + resultSet.close(); + } + if (statement != null) { + statement.close(); + } + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + } +}