package com.atgaozhen.jdbc;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCTool {
private InputStream resultSet;
public void CloseFunction2(Connection
connection,Statement statement
) throws SQLException{
try {
if(statement!=null)
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection!=null)
connection.close();
}
}
//4.執行update
public void updatesql(String sql) throws IOException,
SQLException, ClassNotFoundException{
Connection connection=null;
Statement statement=null;
try {
connection=getConnection();
statement=(Statement) connection.createStatement();
statement.executeUpdate(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
CloseFunction2(connection,statement);
}
}
//3.執行sql
public void executeselect(String sql) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException{
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
try {
connection=getConnection();
statement=connection.createStatement();
resultSet=statement.executeQuery(sql);
if(resultSet.next()){
int id=resultSet.getInt(1);
String username=resultSet.getString(2);
String password=resultSet.getString(3);
System.out.println("id:"+id);
System.out.println("username:"+username);
System.out.println("password:"+password);
}
//关闭数据库资源
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
CloseFunction(
connection,statement,
resultSet);
}
}
//2.关闭资源的方法
public void CloseFunction(Connection
connection,Statement statement,
ResultSet resultSet) throws SQLException{
try {
if(resultSet!=null)
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(statement!=null)
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection!=null)
connection.close();
}
}
}
//1.获取connection的方法
public Connection getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException {
String url=null;
String user=null;
String password=null;
String name=null;
//1.读取类路径下的jdbc.properties文件
InputStream in=
getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(in);
name=properties.getProperty("name");
url=properties.getProperty("url");
user=properties.getProperty("user");
password=properties.getProperty("password");
Driver driver=(Driver) Class.forName(name).newInstance();
Properties info=new Properties();
info.put("user",user);
info.put("password",password);
Connection connection=driver.connect(url, info);
return connection;
//System.out.println("通用Driver connection: "+connection);
}
//可变参数
public void update(String sql, Object ... argu) throws SQLException {
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection=new JDBCTool().getConnection();
preparedStatement=connection.prepareStatement(sql);
for(int i=0;i<argu.length;++i){
preparedStatement.setObject(i+1, argu[i]);
}
System.out.println(sql);
preparedStatement.executeUpdate();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
new JDBCTool().CloseFunction(connection, preparedStatement, null);
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCTool {
private InputStream resultSet;
public void CloseFunction2(Connection
connection,Statement statement
) throws SQLException{
try {
if(statement!=null)
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection!=null)
connection.close();
}
}
//4.執行update
public void updatesql(String sql) throws IOException,
SQLException, ClassNotFoundException{
Connection connection=null;
Statement statement=null;
try {
connection=getConnection();
statement=(Statement) connection.createStatement();
statement.executeUpdate(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
CloseFunction2(connection,statement);
}
}
//3.執行sql
public void executeselect(String sql) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException{
Connection connection=null;
Statement statement=null;
ResultSet resultSet=null;
try {
connection=getConnection();
statement=connection.createStatement();
resultSet=statement.executeQuery(sql);
if(resultSet.next()){
int id=resultSet.getInt(1);
String username=resultSet.getString(2);
String password=resultSet.getString(3);
System.out.println("id:"+id);
System.out.println("username:"+username);
System.out.println("password:"+password);
}
//关闭数据库资源
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
CloseFunction(
connection,statement,
resultSet);
}
}
//2.关闭资源的方法
public void CloseFunction(Connection
connection,Statement statement,
ResultSet resultSet) throws SQLException{
try {
if(resultSet!=null)
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(statement!=null)
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection!=null)
connection.close();
}
}
}
//1.获取connection的方法
public Connection getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException {
String url=null;
String user=null;
String password=null;
String name=null;
//1.读取类路径下的jdbc.properties文件
InputStream in=
getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(in);
name=properties.getProperty("name");
url=properties.getProperty("url");
user=properties.getProperty("user");
password=properties.getProperty("password");
Driver driver=(Driver) Class.forName(name).newInstance();
Properties info=new Properties();
info.put("user",user);
info.put("password",password);
Connection connection=driver.connect(url, info);
return connection;
//System.out.println("通用Driver connection: "+connection);
}
//可变参数
public void update(String sql, Object ... argu) throws SQLException {
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection=new JDBCTool().getConnection();
preparedStatement=connection.prepareStatement(sql);
for(int i=0;i<argu.length;++i){
preparedStatement.setObject(i+1, argu[i]);
}
System.out.println(sql);
preparedStatement.executeUpdate();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
new JDBCTool().CloseFunction(connection, preparedStatement, null);
}
}
}