來源:gdjlc 發(fā)布時間:2018-11-21 14:06:30 閱讀量:982
1、數(shù)據(jù)庫配置文件jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=
2、數(shù)據(jù)庫數(shù)據(jù)類JdbcUtils
package com.test;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcUtils {
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null;
// 加載驅(qū)動,獲取數(shù)據(jù)庫連接信息
static {
try {
// 加載配置文件
InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("DB.properties");
Properties properties = new Properties();
properties.load(in);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
// 加載驅(qū)動
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 獲取數(shù)據(jù)庫連接
* @throws SQLException
*/
public static Connection getConnection() {
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 釋放資源
* @param connection
* @param preparedStatement
* @param resultSet
*/
public static void releaseDB(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3、增刪改查
package com.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JdbcTest {
public void insert() {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
// 獲取連接
connection = JdbcUtils.getConnection();
// 準備sql語句
String sql = "INSERT INTO user(id,name,age) VALUES(?,?,?)";
// 獲取PrepareStatement對象
preparedStatement = connection.prepareStatement(sql);
// 填充占位符
preparedStatement.setInt(1, 6);
preparedStatement.setString(2, "小A");
preparedStatement.setInt(3, 25);
// 執(zhí)行sql
int num = preparedStatement.executeUpdate();// 返回影響到的行數(shù)
System.out.println("一共影響到" + num + "行");
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.releaseDB(connection, preparedStatement, null);
}
}
public void update() {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
// 獲取連接
connection = JdbcUtils.getConnection();
// 準備sql語句
String sql = "update user set age = ? where name = ?";
// 獲取PrepareStatement對象
preparedStatement = connection.prepareStatement(sql);
// 填充占位符
preparedStatement.setInt(1, 30);
preparedStatement.setString(2, "小A");
// 執(zhí)行sql
int num = preparedStatement.executeUpdate();// 返回影響到的行數(shù)
System.out.println("一共影響到" + num + "行");
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.releaseDB(connection, preparedStatement, null);
}
}
public void query() {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JdbcUtils.getConnection();
String sql = "SELECT name,age FROM user";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
// 遍歷結(jié)果集
while (resultSet.next()) {
String name = resultSet.getString(1);
Integer age = resultSet.getInt(2);
System.out.println("name:" + name + ", age:" + age);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.releaseDB(connection, preparedStatement, resultSet);
}
}
public void delete() {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JdbcUtils.getConnection();
String sql = "DELETE FROM user WHERE id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 6);
int num = preparedStatement.executeUpdate();
System.out.println("一共影響到" + num + "行");
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.releaseDB(connection, preparedStatement, null);
}
}
}
---------------------