Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.diven.game.db;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.Reader;
- import java.lang.reflect.Method;
- import java.math.BigDecimal;
- import java.sql.Array;
- import java.sql.Blob;
- import java.sql.CallableStatement;
- import java.sql.Clob;
- import java.sql.Date;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Time;
- import java.sql.Timestamp;
- import java.util.ArrayList;
- import java.util.List;
- import com.diven.game.common.CommonMethod;
- import com.diven.game.common.SqlConfig;
- /**
- * procedure operate
- * @author Administrator
- */
- public class DbProc {
- public java.sql.Connection conn = null;
- public ResultSet rs = null;
- public CallableStatement callProc = null;
- public String _dirver = "oracle.jdbc.driver.OracleDriver";
- public DbProc() {
- try {
- //conn = InitServlet.getDs().getConnection();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public DbProc(String sql) {
- try {
- //conn = InitServlet.getDs().getConnection();
- callProc = conn.prepareCall(sql);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- /**
- * test
- *
- * @param sql
- * @param str
- */
- public DbProc(String sql, String str) {
- try {
- String url = "jdbc:oracle:thin:@127.0.0.1:1521:xiaoyi";
- String user = "qbxt";
- String password = "qbxt";
- Class.forName(_dirver);
- conn = DriverManager.getConnection(url, user, password);
- callProc = conn.prepareCall(sql);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public DbProc(String sql,int test) {
- try {
- String url = "jdbc:sqlserver://116.93.102.116:1433;DatabaseName=dvtest";
- String user = "diven";
- String password = "diven";
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- conn = DriverManager.getConnection(url, user, password);
- callProc = conn.prepareCall(sql);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public DbProc(SqlConfig config, String sql) {
- try {
- // String drivers = "oracle.jdbc.driver.OracleDriver";
- String url = config.getDbsource();//"jdbc:oracle:thin:@127.0.0.1:1521:xiaoyi";
- String user = config.getUserid();//"qbxt";
- String password = config.getPwd();//"qbxt";
- Class.forName(_dirver);
- conn = DriverManager.getConnection(url, user, password);
- callProc = conn.prepareCall(sql);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public DbProc(SqlConfig config) {
- try {
- //String drivers = "oracle.jdbc.driver.OracleDriver";
- String url = config.getDbsource();//"jdbc:oracle:thin:@127.0.0.1:1521:xiaoyi";
- String user = config.getUserid();//"qbxt";
- String password = config.getPwd();//"qbxt";
- Class.forName(_dirver);
- conn = DriverManager.getConnection(url, user, password);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public DbProc(String dirv,SqlConfig config) {
- try {
- _dirver = dirv;
- String url = config.getDbsource();//"jdbc:oracle:thin:@127.0.0.1:1521:xiaoyi";
- String user = config.getUserid();//"qbxt";
- String password = config.getPwd();//"qbxt";
- Class.forName(_dirver);
- conn = DriverManager.getConnection(url, user, password);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- public boolean setSql(String sql)throws Exception{
- return setProcSql(sql);
- }
- public boolean setProcSql(String sql) throws Exception {
- if (conn != null) {
- callProc = conn.prepareCall(sql);
- return true;
- } else {
- //conn = InitServlet.getDs().getConnection();
- callProc = conn.prepareCall(sql);
- return true;
- }
- }
- /**
- * execute
- *
- * @throws SQLException
- */
- public void executeUpdate() throws SQLException {
- if (callProc == null)
- return;
- callProc.executeUpdate();
- }
- public boolean execute() throws SQLException {
- if (callProc == null)
- return false;
- return callProc.execute();
- }
- public Object getObject(int index) throws SQLException {
- if (callProc == null)
- return null;
- return callProc.getObject(index);
- }
- public ResultSet executeQuery() throws SQLException {
- if (callProc == null)
- return null;
- rs = callProc.executeQuery();
- return rs;
- }
- /**
- * 返回单个数据(INT i OUT的位�?
- * @return
- * @throws Exception
- */
- public Object exec(int i) throws Exception{
- if (callProc == null)
- return new Object();
- callProc.execute();
- return callProc.getObject(i);
- }
- public Object get_one() throws Exception{
- if (callProc == null)
- return new Object();
- rs = callProc.executeQuery();
- if(rs.next()){
- return rs.getObject(1);
- }
- return null;
- }
- public <T> List<T> execExt(Class<T> cls,int index) throws Exception{
- List<T> list = new ArrayList<T>();
- if (callProc == null)
- return list;
- ResultSet rs = callProc.executeQuery();
- //rs = (ResultSet)callProc.getObject(index);
- return toList(rs,cls);
- }
- /**
- * 单个列表
- * @param cls
- * @param index
- * @return
- * @throws Exception
- */
- public <T> List<T> exec(Class<T> cls,int index) throws Exception{
- List<T> list = new ArrayList<T>();
- if (callProc == null)
- return list;
- callProc.execute();
- rs = (ResultSet)callProc.getObject(index);
- return toList(rs,cls);
- }
- public void exec()throws Exception{
- if (callProc == null)
- return;
- callProc.execute();
- }
- public <T> List<T> getResult(Class<T> cls,int index)throws Exception{
- rs = (ResultSet)callProc.getObject(index);
- return toList(rs,cls);
- }
- public Object getResult(int index) throws SQLException{
- return callProc.getObject(index);
- }
- public String queryClob()throws SQLException, IOException{
- String dstr = null;
- if (callProc == null)
- return dstr;
- rs = callProc.executeQuery();
- if(rs.next()){
- java.sql.Clob c = rs.getClob(1);
- Reader inStream = c.getCharacterStream();
- char[] cr = new char[(int)c.length()];
- inStream.read(cr);
- dstr = new String(cr);
- inStream.close();
- }
- rs.close();
- return dstr;
- }
- public Blob queryBlob()throws SQLException, IOException{
- if (callProc == null)
- return null;
- rs = callProc.executeQuery();
- java.sql.Blob blob=null;
- if(rs.next()){
- blob = rs.getBlob(1);
- }
- rs.close();
- return blob;
- }
- public <T> List<T> toList(ResultSet rs, Class<T> cls) throws Exception{
- List<T> list = new ArrayList<T>();
- try {
- ResultSetMetaData meta = rs.getMetaData();
- T obj = null;
- while (rs.next()) {
- obj = cls.newInstance();//(T) Class.forName(cls.getName()).newInstance();
- Method[] meth = obj.getClass().getMethods();
- for (int i = 1; i <= meta.getColumnCount(); i++) {
- String colName = meta.getColumnName(i);
- colName = new String(colName.charAt(0)+"").toUpperCase()+""+colName.substring(1).toLowerCase();
- //colName = colName.replace(colName.charAt(0) + "",new String(colName.charAt(0) + "").toUpperCase());
- String methodName = "set" + colName;
- boolean flag = false;
- Class<?> c = null;
- for(Method m:meth){
- if(methodName.equals(m.getName())){
- c = m.getParameterTypes()[0];
- flag=true;
- break;
- }
- }
- if(!flag){
- continue;
- }
- //////////////////////////////////////////////////////////////
- Object value = (Object)rs.getObject(i);
- if(value != null){
- if("BigDecimal".equals(value.getClass().getSimpleName())){
- if("java.lang.Float".equals(c.getName())|| "float".equals(c.getName())){
- value=Float.parseFloat(value.toString());
- }else if("java.lang.Long".equals(c.getName()) || "long".equals(c.getName())){
- double diven = Double.parseDouble(value.toString());
- value = (long)Math.floor(diven);
- }else if("java.lang.Double".equals(c.getName())){
- value = Double.parseDouble(value.toString());
- }else if("java.lang.String".equals(c.getName())){
- value=value.toString();
- }
- }else if("Date".equals(value.getClass().getSimpleName())){
- value = rs.getString(i).substring(0, 19);
- }else if("Timestamp".equals(value.getClass().getSimpleName())){
- //value = value.toString();
- value = CommonMethod.TimestampFormat((Timestamp)value,"yyyy-MM-dd HH:mm:ss");
- }else if("Integer".equals(value.getClass().getSimpleName())){
- if("java.lang.Float".equals(c.getName())){
- value=Float.parseFloat(value.toString());
- }else if("java.lang.Long".equals(c.getName())){
- value=Long.parseLong(value.toString());
- }else if("java.lang.String".equals(c.getName())){
- value=value.toString();
- }
- }
- }
- //System.out.println(value);
- Method method = obj.getClass().getMethod(methodName,c);
- method.invoke(obj, value);
- }
- list.add(obj);
- }
- }catch(Exception ex) {
- ex.printStackTrace();
- }finally{
- try{
- rs.close();
- }catch(Exception ex){
- throw new Exception(ex.getMessage());
- }
- }
- return list;
- }
- /**
- * set auto commit for roll back
- *
- * @param value
- * @throws SQLException
- */
- public void setAutoCommit(boolean value) throws SQLException {
- if (this.conn != null)
- this.conn.setAutoCommit(value);
- }
- /**
- * commit current operate
- *
- * @throws SQLException
- */
- public void commit() throws SQLException {
- this.conn.commit();
- }
- /**
- * @function roll back operate
- * @throws SQLException
- */
- public void rollback() throws SQLException {
- this.conn.rollback();
- }
- public void Close(){
- close();
- }
- public void close() {
- try {
- if (rs != null) {
- rs.close();
- rs = null;
- }
- } catch (Exception e) {
- System.out.println("close rs error!");
- } finally {
- try {
- if (callProc != null) {
- callProc.close();
- callProc = null;
- }
- } catch (Exception e) {
- System.out.println("close callProc error!");
- } finally {
- try {
- if (conn != null) {
- conn.close();
- conn = null;
- }
- } catch (Exception e) {
- System.out.println("close conn error!");
- }
- }
- }
- }
- public void setString(int index, String value) throws SQLException {
- callProc.setString(index, value);
- }
- public void setLong(int index, Long value) throws SQLException {
- callProc.setLong(index, value);
- }
- public void setInt(int index, int value) throws SQLException {
- callProc.setInt(index, value);
- }
- public void setArray(int index, Array value) throws SQLException {
- callProc.setArray(index, value);
- }
- public void setDouble(int index, Double value) throws SQLException {
- callProc.setDouble(index, value);
- }
- public void setFloat(int index, float value) throws SQLException {
- callProc.setFloat(index, value);
- }
- public void addBatch(String value) throws SQLException {
- callProc.addBatch(value);
- }
- public void setTime(int index, Time value) throws SQLException {
- callProc.setTime(index, value);
- }
- public void setTimestamp(int index, Timestamp value) throws SQLException {
- callProc.setTimestamp(index, value);
- }
- public void setTimestamp(int index, java.util.Date date) throws SQLException {
- Timestamp tstamp = new Timestamp(date.getTime());
- callProc.setTimestamp(index, tstamp);
- }
- public void setTimestamp(int index, String value,String type) throws SQLException {
- java.util.Date dt = CommonMethod.strToDate(value, type);
- Timestamp tstamp = new Timestamp(dt.getTime());
- callProc.setTimestamp(index, tstamp);
- }
- public void setTimestamp(int index, String value) throws SQLException {
- java.util.Date dt = CommonMethod.strToDate(value,"yyyy-MM-dd HH:mm:ss");
- Timestamp tstamp = new Timestamp(dt.getTime());
- callProc.setTimestamp(index, tstamp);
- }
- public void setDate(int index, Date value) throws SQLException {
- callProc.setDate(index, value);
- }
- public void setBoolean(int index, boolean value) throws SQLException {
- callProc.setBoolean(index, value);
- }
- public void setOutParam(int index, int value) throws SQLException {
- callProc.registerOutParameter(index, value);
- }
- public void setBytes(int index, byte[] value) throws SQLException {
- callProc.setBytes(index, value);
- }
- public void setBigDecimal(int index,int n) throws SQLException{
- BigDecimal bgd = new BigDecimal(n);
- callProc.setBigDecimal(index, bgd);
- }
- public void setBigDecimal(int index,long n) throws SQLException{
- BigDecimal bgd = new BigDecimal(n);
- callProc.setBigDecimal(index, bgd);
- }
- public void setBinaryStream(int index, InputStream value, int len)
- throws SQLException {
- callProc.setBinaryStream(index, value, len);
- }
- public void setClob(int index, Clob value) throws SQLException {
- callProc.setClob(index, value);
- }
- public void setBlob(int index, Blob value) throws SQLException {
- callProc.setBlob(index, value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement