工具类封装
public class JDBCUtils {
private static String user;
private static String password;
private static String url;
private static String driver;
static {
try {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driver = properties.getProperty("driver");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection(){
try {
Class.forName(driver);
return DriverManager.getConnection(url,user,password);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void close(ResultSet set, Statement statement, Connection connection){
try {
if(set != null){
set.close();
}
if(statement != null){
statement.close();
}
if(connection != null){
connection.close();
}
} catch (SQLException e){
throw new RuntimeException(e);
}
}
}
调用
public class JDBCUtils_Use {
@Test
public void testDML() throws Exception{
Connection connection = JDBCUtils.getConnection();
String sql = "update news set content = ? where id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,"ybj is a man");
preparedStatement.setInt(2,3);
int rows = preparedStatement.executeUpdate();
System.out.println(rows > 0 ? "true" : "false");
JDBCUtils.close(null,preparedStatement,connection);
}
@Test
public void testSelect() throws Exception{
Connection connection = JDBCUtils.getConnection();
String sql = "select * from news";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
int id = resultSet.getInt("id");
String content = resultSet.getString("content");
System.out.println(id + "\t" + content);
}
JDBCUtils.close(resultSet,preparedStatement,connection);
}
}
Druid工具类
public class JDBCUtilsByDruid {
private static DataSource ds;
static {
Properties properties = new Properties();
try {
properties.load(new FileInputStream("src\\druid.properties"));
ds = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
public static void closeConnection(ResultSet resultSet, Connection connection, Statement statement) {
try {
if(resultSet != null){
resultSet.close();
}
if (connection != null){
connection.close();
}
if(statement != null){
statement.close();
}
}catch (SQLException e){
throw new RuntimeException(e);
}
}
}
调用
public class JDBCUtilsDruid_Use {
public void testDML() throws Exception{
Connection connection = JDBCUtilsByDruid.getConnection();
String sql = "insert into news set content = ? where id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,"cyt");
preparedStatement.setInt(2,1);
preparedStatement.executeUpdate();
JDBCUtilsByDruid.closeConnection(null,connection,preparedStatement);
}
public void testSelect() throws Exception{
Connection connection = JDBCUtilsByDruid.getConnection();
String sql = "select * from news";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
int id = resultSet.getInt("id");
String content = resultSet.getString("content");
System.out.println(id + "\t" + content);
}
JDBCUtilsByDruid.closeConnection(resultSet,connection,preparedStatement);
}
}