package com.local.util; import java.sql.*; /** * @date 2023/4/15 18:13 * @description */ public class JdbcUtil { public static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver"; public static final String URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8"; public static final String USER = "root"; public static final String PASSWORD = ""; public static ResultSet select(String sql) { Connection connection = newConnection(); try { Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql); return resultSet; } catch (SQLException e) { e.printStackTrace(); } return null; } public static void insert() {} public static void update() {} public static void delete() {} public static Connection newConnection() { return newConnection(URL, USER, PASSWORD); } public static Connection newConnection(String url, String user, String password) { try { Class.forName(DRIVER_NAME); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { Connection connection = DriverManager.getConnection(url, user, password); return connection; } catch (SQLException e) { e.printStackTrace(); } return null; } public static void close(ResultSet resultSet, Statement statement, Connection connection) { try { resultSet.close(); statement.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); } } }
标签:jdbc,return,String,编程,基础,Connection,connection,static,public From: https://www.cnblogs.com/benjerry/p/17321695.html