spring boot配置第二个数据库
package com.microvideo.ewcp.configration;
import java.sql.*;
public class DataBaseSql {
static Connection conn;
public static void insert(String sql) throws SQLException {
conn = getConnection();
try {
Statement statement = conn.createStatement();
int count = statement.executeUpdate(sql);
System.out.println("向表中插入 " + count + " 条数据");
conn.close();
} catch (SQLException e) {
System.out.println("插入数据失败" + e.getMessage());
}
}
public static void update(String sql) throws SQLException {
conn = getConnection();
try {
Statement statement = conn.createStatement();
int count = statement.executeUpdate(sql);
System.out.println("表中更新 " + count + " 条数据");
conn.close();
} catch (SQLException e) {
System.out.println("更新数据失败");
}
}
public static void query(String sql) throws SQLException {
conn = getConnection();
try {
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sql);
while (rs.next()){
}
conn.close();
} catch (SQLException e) {
System.out.println("查询数据失败");
}
}
public static void delete(String sql) throws SQLException {
conn = getConnection();
try {
Statement statement = conn.createStatement();
int count = statement.executeUpdate(sql);
System.out.println("表中删除 " + count + " 条数据\n");
conn.close();
} catch (SQLException e) {
System.out.println("删除数据失败");
}
}
public static Connection getConnection() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:30806/yc_external?useUnicode=true&characterEncoding=utf-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8",
"external", "ctfo@2022");
} catch (Exception e) {
System.out.println("数据库连接失败" + e.getMessage());
}
return con;
}
}