package org.demo.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class BaseDao {
/**第一种连接,获得connecton的方法
private static Connection getConnection() throws ClassNotFoundException,SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore",
"root", "root");
return connection;
}
*/
/**以下是第二种连接数据库的方法,个人建议推荐使用方便点*/
private static MysqlDataSource dataSource;
static {
dataSource = new MysqlDataSource();
dataSource.setDatabaseName("bookstore");
dataSource.setUser("root");
dataSource.setPassword("root");
dataSource.setPort(3306);
}
private static Connection getConnection() throws ClassNotFoundException,SQLException {
return dataSource.getConnection();
}
public static void executeUpdate(String sql, Object... params)
throws ClassNotFoundException, SQLException {
// 获得连接
Connection connection = getConnection();
// 获得预处理statement
PreparedStatement statement = connection.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
statement.setObject(i + 1, params[i]);
}
}
//执行操作
statement.executeUpdate();
}
//关闭连接,此方法主要针对增,删,改的关闭
private static void closeResource(ResultSet resultSet,Statement statement,Connection connection){
try {
if(null!=resultSet){
resultSet.close();
}
if(null!=statement){
statement.close();
}
if(null!=connection){
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//针对查询数据的方法
public static ResultSet executeQuery(String sql, Object... params) throws ClassNotFoundException, SQLException{
Connection connection=getConnection();
PreparedStatement statement=connection.prepareStatement(sql);
if(null!=params && params.length>0){
for (int i = 0; i < params.length; i++) {
statement.setObject(i+1, params[i]);
}
}
return statement.executeQuery();
}
//针对查询写的关闭
public static void closeResource(ResultSet resultSet){
try {
Statement statement=resultSet.getStatement();
Connection connection=statement.getConnection();
if(null!=resultSet){
resultSet.close();
}
if(null!=statement){
statement.close();
}
if(null!=connection){
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class BaseDao {
/**第一种连接,获得connecton的方法
private static Connection getConnection() throws ClassNotFoundException,SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore",
"root", "root");
return connection;
}
*/
/**以下是第二种连接数据库的方法,个人建议推荐使用方便点*/
private static MysqlDataSource dataSource;
static {
dataSource = new MysqlDataSource();
dataSource.setDatabaseName("bookstore");
dataSource.setUser("root");
dataSource.setPassword("root");
dataSource.setPort(3306);
}
private static Connection getConnection() throws ClassNotFoundException,SQLException {
return dataSource.getConnection();
}
public static void executeUpdate(String sql, Object... params)
throws ClassNotFoundException, SQLException {
// 获得连接
Connection connection = getConnection();
// 获得预处理statement
PreparedStatement statement = connection.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
statement.setObject(i + 1, params[i]);
}
}
//执行操作
statement.executeUpdate();
}
//关闭连接,此方法主要针对增,删,改的关闭
private static void closeResource(ResultSet resultSet,Statement statement,Connection connection){
try {
if(null!=resultSet){
resultSet.close();
}
if(null!=statement){
statement.close();
}
if(null!=connection){
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//针对查询数据的方法
public static ResultSet executeQuery(String sql, Object... params) throws ClassNotFoundException, SQLException{
Connection connection=getConnection();
PreparedStatement statement=connection.prepareStatement(sql);
if(null!=params && params.length>0){
for (int i = 0; i < params.length; i++) {
statement.setObject(i+1, params[i]);
}
}
return statement.executeQuery();
}
//针对查询写的关闭
public static void closeResource(ResultSet resultSet){
try {
Statement statement=resultSet.getStatement();
Connection connection=statement.getConnection();
if(null!=resultSet){
resultSet.close();
}
if(null!=statement){
statement.close();
}
if(null!=connection){
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}