JAVA JDBC工具类
JDBC工具类
创建一个properties 文件
Driver = com.mysql.jdbc.Driver url = jdbc:mysql://localhost:3306/study?useSSL=true username = wdnmd password = 123
package wdnmd.xswl; import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.sql.*; import java.util.Properties; public class JDBCUtils { private static String url; private static String username; private static String password; private static String driver; static { try { //读取资源文件,获取值 Properties properties = new Properties(); ClassLoader classLoader = JDBCUtils.class.getClassLoader(); URL URL = classLoader.getResource("jdbc.properties"); String path = URL.getPath(); properties.load(new FileReader(path)); //获取数据,赋值 url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); driver = properties.getProperty("Driver"); Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url,username,password); } /* 释放资源 */ public static void close(Statement statement,Connection connection){ if (statement != null){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet resultSet, Statement statement, Connection connection){ if (resultSet != null){ try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet resultSet, Connection connection){ if (resultSet != null){ try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
测试
public static void main(String[] args) throws SQLException { Connection connection = JDBCUtils.getConnection(); String sql = "select * from sort"; ResultSet resultSet = connection.prepareStatement(sql).executeQuery(); while (resultSet.next()){ System.out.println(resultSet.getString("sname")); } JDBCUtils.close(resultSet,connection); }