JDBCUtil工具类
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
static{
try{
Properties properties=new Properties();
ClassLoader classLoader=JDBCUtils.class.getClassLoader();
URL res=classLoader.getResource("jdbc.properties");
String path=res.getPath();
System.out.println(path);
properties.load(new FileReader("src/jdbc.properties"));
url= properties.getProperty(url);
user=properties.getProperty(user);
password=properties.getProperty(password);
driver=properties.getProperty(driver);
Class.forName(driver);
}catch (IOException | ClassNotFoundException e){
e.printStackTrace();
}
}
public static Connection getconnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
public static void close(Statement statement, Connection conn) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
pro.Properties
url=jdbc:mysql:/
user=root
password=123456
driver=com.mysql.cj.jdbc.Driver