parent
562f616e64
commit
1007ca363a
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue