博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简易sqlhelper-java
阅读量:7233 次
发布时间:2019-06-29

本文共 5140 字,大约阅读时间需要 17 分钟。

hot3.png

package com.pas.util;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.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 driver = "";	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();			//类加载器默认主目录SRC			fis = SqlHelper.class.getClassLoader().getResourceAsStream(					"dbInfo.properties");			// fis = new FileInputStream();			pp.load(fis);			url = pp.getProperty("url");			username = pp.getProperty("username");			driver = pp.getProperty("driver");			passwd = pp.getProperty("passwd");			Class.forName(driver);		} 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;	}	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 int 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]);				}			}			return 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;		}	}}

需要添加dbInfo.properties文件在src目录下:

url=jdbc:oracle:thin:@127.0.0.1:1521:ORCLusername=scottdriver=oracle.jdbc.driver.OracleDriverpasswd=654123

转载于:https://my.oschina.net/u/1246663/blog/165512

你可能感兴趣的文章
利用fdisk将硬盘剩余空间进行分区
查看>>
解决键盘挡住部分组件 降低用户体验的解决办法
查看>>
开启Cisco交换机DHCP Snooping功能
查看>>
linux中目录的说明小结
查看>>
使用sublime同步编辑线上脚本
查看>>
new和delete
查看>>
Exchange 2010 – NLB Client Access (on Virtual Machines)
查看>>
Lync Server 2010迁移至Lync Server 2013部署系列 Part15:迁移中央管理器(CMS)
查看>>
HIVE中的自定义函数
查看>>
我的友情链接
查看>>
35场面试,应届计算机毕业生的霸气求职之路
查看>>
通过IP来获取位置
查看>>
数据分析入门--https://cn.udacity.com/
查看>>
http://blog.sina.com.cn/s/blog_5fde608901010pl5.html
查看>>
Nginx+Tomcat搭建高性能负载均衡集群
查看>>
勒索病毒防范方案-有韩立刚老师总结的非常规手段
查看>>
三层交换及动态路由,半臂路由
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
SCCM 2016 使用PXE 部署操作系统(五)
查看>>