这里介绍一个连接数据库的工具类SqlHelper(自己写的).SqlHelper的作用是将对数据库的crud封装起来,简化逻辑,提高代码的利用率.当别的页面需要对数据库进行crud时,直接调用SqlHelper就可以了.不多说直接上传代码.
package com.tools;
import java.sql.*;
import java.io.*;
import java.util.*;
public class SqlHelper {
//定义需要的变量
private Connection conn=null;
private PreparedStatement ps=null;
private ResultSet rs=null;
private CallableStatement cs=null;
private static String driver="";
private static String url="";
private static String DbUser="";
private static String DbPasswd="";
//读取配置文件
private static Properties pp=null;
private static InputStream in=null;
//Class只是加载了一次
static {
try {
pp=new Properties();
in=SqlHelper.class.getClassLoader().getResourceAsStream("dbinfo.properties");
pp.load(in);
driver=pp.getProperty("driver");
url=pp.getProperty("url");
DbUser=pp.getProperty("DbUser");
DbPasswd=pp.getProperty("DbPasswd");
Class.forName(driver);
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
in.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
//得到连接
private Connection getConnection() {
try {
conn=DriverManager.getConnection(url,DbUser,DbPasswd);
}catch(Exception e) {
e.printStackTrace();
}
return conn;
}
//调用存储过程,有返回Result
public CallableStatement callPro2(String sql,
String []inparameters,String []outparameters) {
try {
conn=getConnection();
cs=conn.prepareCall(sql);
if(inparameters!=null) {
for(int i=0;i<inparameters.length;i++) {
cs.setObject(i+1,inparameters[i]);
}
}
//给out参数赋值
if(outparameters!=null) {
for(int i=0;i<outparameters.length;i++) {
cs.registerOutParameter(inparameters.length+1+i,outparameters[i]);
}
}
cs.execute();
}catch(Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally {
//不需要关闭,但是最好在那里使用在那里关闭
}
return cs;
}
public void callPro1(String sql,Object...args) {
try {
conn=getConnection();
cs=conn.prepareCall(sql);
for(int i=0;i<args.length;i++) {
cs.setObject(i+1,args[i]);
}
cs.execute();
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally {
close(rs,cs,conn);
}
}
//统一的select
//ResultSet->ArrayList
public ResultSet executeQuery(String sql,Object...args) {
try {
conn=getConnection();
conn.prepareStatement(sql);
if(args!=null&&!args.equals("")) {
for(int i=0;i<args.length;i++)
ps.setObject(i+1,args[i]);
}
rs=ps.executeQuery();
}catch(Exception e){
e.printStackTrace();
}finally {
//在这里不关闭
}
return rs;
}
//这也是一个查询的方法的升级...
//遵循在那里用资源,在那里关闭资源
public ArrayList executeQuery2(String sql,Object...args) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
conn=getConnection();
ps=conn.prepareStatement(sql);
//对问号?赋值
if(args!=null&&!args.equals("")) {
for(int i=0;i<args.length;i++)
ps.setObject(i+1,args[i]);
}
rs=ps.executeQuery();
ArrayList al=new ArrayList();
ResultSetMetaData rsmd=rs.getMetaData();
//这里可以得到你查询得到多少列
int column=rsmd.getColumnCount();
while(rs.next()) {
Object []ob=new Object[column];
for(int i=1;i<=column;i++) {
ob[i-1]=rs.getObject(i);
}
al.add(ob);
}
return al;
}catch(Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally {
close(rs,ps,conn);
}
}
//如果有多个update/delete/insert(需要考虑事务)
public void exectueUpdate2(String sql[],String [][]parameters) {
try {
conn=getConnection();
//关闭自动提交
conn.setAutoCommit(false);
for(int i=0;i<sql.length;i++) {
ps=conn.prepareStatement(sql[i]);
if(parameters[i]!=null) {
for(int j=0;j<parameters[i].length;j++){
ps.setObject(j+1,parameters[i][j]);
}
}
ps.executeUpdate();
}
conn.commit();
}catch(Exception e) {
e.printStackTrace();
//回滚
try {
conn.rollback();
}catch(SQLException e1) {
e1.printStackTrace();
}
throw new RuntimeException(e.getMessage());
}finally {
close(rs,ps,conn);
}
}
//这个是一个update/insert/delete
public void executeUpdate(String sql,Object...args) {
try {
conn=getConnection();
ps=conn.prepareStatement(sql);
if(args!=null) {
for(int i=0;i<args.length;i++)
ps.setObject(i+1,args[i]);
}
ps.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally {
close(rs,ps,conn);
}
}
public void close(ResultSet rs,Statement ps,Connection conn) {
if(rs!=null) {
try {
rs.close();
}catch(Exception e) {
e.printStackTrace();
}
rs=null;
}
if(ps!=null) {
try {
ps.close();
}catch(Exception e) {
e.printStackTrace();
}
ps=null;
}
if(conn!=null) {
try {
conn.close();
}catch(Exception e) {
e.printStackTrace();
}
conn=null;
}
}
public Connection getConn() {
return conn;
}
public PreparedStatement getPs() {
return ps;
}
public ResultSet getRs() {
return rs;
}
public CallableStatement getCs() {
return cs;
}
}
这个SqlHelper读取properties配置文件中信息获取数据库的驱动,连接数据库的URL,登录数据库的帐号,密码.
dbinfo.properties中的信息如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/school
DbUser=root
DbPasswd=root
这样做的好处是:当需要修改驱动信息,url信息等信息时,不必修改SqlHelper中的源代码,只要修改dbinfo.properties中的信息就可以了.
在这里需要提一下:SqlHelper这个工具类和dbinfo.properties都是在同一个src目录下的,如果不将它们放在一起,会报一个找不到dbinfo.properties系统文件路径的错误.