首页 技术 正文
技术 2022年11月20日
0 收藏 688 点赞 2,886 浏览 5074 个字

dbinfo.properties部分:

注意每行末尾不可以有空格

#oracle configure
UserName=scott
Password=tiger
Driver=oracle.jdbc.driver.OracleDriver
Url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
#mysql
#dbUserName=root
#dbPassword=root
#dbDriver=com.mysql.jdbc.Driver
#dbUrl=jdbc\:mysql\://127.0.0.1\:3306/test100?useUnicode\=true&characterEncoding\=utf8

sqlhelper部分:

package com.dy.utils;import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;
import java.sql.*;
public class SqlHelper
{
//定义变量
private static Connection ct = null;
//大多数情况下用preparedstatement替代statement
private static PreparedStatement ps = null;
private static ResultSet rs = null; //连接数据库的参数
private static String url = "";
private static String username = "";
private static String drivername = "";
private static String passwd = ""; private static CallableStatement cs = null;
public static CallableStatement getCs()
{
return cs;
}
private static Properties pp = null;
private static InputStream fis = null;
//加载驱动,只需要一次,用静态代码块
static
{
try
{
//从dbinfo.properties
pp = new Properties();
fis=SqlHelper.class.getClassLoader().getResourceAsStream("dbinfo.properties");
//fis = new FileInputStream();
pp.load(fis);
url = pp.getProperty("Url");
drivername = pp.getProperty("Driver");
username = pp.getProperty("UserName");
passwd = pp.getProperty("Password");
Class.forName(drivername) ;
}
catch (Exception e)
{
e.printStackTrace(); }
finally
{
try
{ fis.close();}
catch(IOException e) {e.printStackTrace();}
fis = null;//垃圾回收站上收拾
} }
//得到连接
public static Connection getConnection()
{
try
{ct = DriverManager.getConnection(url,username,passwd);}
catch(Exception e) {e.printStackTrace();}
return ct;
} //*************callPro1存储过程函数1*************
public static CallableStatement callPro1(String sql,String[] parameters)
{
try{
ct = getConnection();
cs = ct.prepareCall(sql);
if(parameters!=null){
for(int i=0;i<parameters.length;i++){
cs.setObject(i+1,parameters[i]);
}
}
cs.execute();
}
catch(Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage());}
finally
{ close(rs,cs,ct);}
return cs;
} //*******************callpro2存储过程2************************
public static CallableStatement callPro2(String sql,String[] inparameters,
Integer[] outparameters)
{
try
{
ct = getConnection();
cs = ct.prepareCall(sql);
if(inparameters!=null)
{
for(int i=0;i<inparameters.length;i++)
{
cs.setObject(i+1,inparameters[i]);
}
}
//cs.registerOutparameter(2,oracle.jdbc.OracleTypes.CURSOR);
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;
} // query update
public static ArrayList executeQuery3(String sql,String[] parms)
{
PreparedStatement pstmt=null;
Connection conn=null;
ResultSet rs=null;
try{
conn=getConnection();
pstmt=conn.prepareStatement(sql);
if(parms!=null&&!parms.equals("")){
for(int i=0;i<parms.length;i++){
pstmt.setString(i+1, parms[i]);
}
}
rs=pstmt.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 {
try{
close(rs,pstmt,conn);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
}
public static ResultSet executeQuery(String sql,String[] parameters)
{
try
{
ct=getConnection();
ps=ct.prepareStatement(sql);
if(parameters!=null)
{
for(int i=0;i<parameters.length;i++)
{
ps.setString(i+1,parameters[i]);
}
}
rs = ps.executeQuery();
}
catch(Exception e)
{
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
finally
{ }
return rs;
} public static Connection getCt()
{
return ct;
}
public static PreparedStatement getPs()
{
return ps;
}
public static ResultSet getRs()
{
return rs;
} public static void executeUpdate2(String[] sql,String[][] parameters)
{
try
{
ct = getConnection();
ct.setAutoCommit(false); for(int i=0;i<sql.length;i++)
{ if(null!=parameters[i])
{
ps = ct.prepareStatement(sql[i]);
for(int j=0;j<parameters[i].length;j++)
{
ps.setString(j+1,parameters[i][j]);
}
ps.executeUpdate();
} } ct.commit(); }catch (Exception e)
{
e.printStackTrace();
try
{
ct.rollback();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
throw new RuntimeException(e.getMessage());
}finally
{
close(rs,ps,ct);
} } //先写一个update、delete、insert
//sql格式:update 表名 set 字段名 =?where 字段=?
//parameter神应该是(”abc“,23)
public static void executeUpdate(String sql,String[] parameters)
{
try
{
ct=getConnection();
ps = ct.prepareStatement(sql);
if(parameters!=null)
{
for(int i=0;i<parameters.length;i++)
{
ps.setString(i+1,parameters[i]);
} }
ps.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();//开发阶段
//抛出异常
//可以处理,也可以不处理
throw new RuntimeException(e.getMessage());
}
finally
{
close(rs,ps,ct);
}
} public static void close(ResultSet rs,Statement ps,Connection ct)
{
//关闭资源(先开后关)
if(rs!=null)
{
try
{
rs.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
rs=null;
}
if(ps!=null)
{
try
{
ps.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
ps=null;
}
if(null!=ct)
{
try
{
ct.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
ct=null;
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,997
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,356
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,139
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848