⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbcserver.java

📁 一个基于java工厂模式的 的实现
💻 JAVA
字号:
/*
 * DAOServer.java
 *
 * Created on 2007年4月6日, 下午2:08
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.ebuy.dataaccess;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import oracle.sql.JAVA_STRUCT;

/**
 *
 * @author Administrator
 */
public class JDBCServer
{
    private static String drivername;
    private static String host;
    private static String port;
    private static String sid;
    private static String uid;
    private static String password;
    private static String url;
    
    private  java.sql.Connection conn;
    private boolean inTrans=false;
    static
    {
        try
        {
            InitDataBaseInfo();
            Class.forName(drivername);
        }
        catch (Exception ex)
        {
            ExceptionManager.handlerException(ex);
        }
    }
    static void InitDataBaseInfo() throws IOException
    {
        DataBaseConfig dbc=new DataBaseConfig();
        Properties prop=dbc.getDataBaseConfigProperties();
        drivername=prop.getProperty("drivername");
        host=prop.getProperty("host");
        port=prop.getProperty("port");
        sid=prop.getProperty("sid");
        uid=prop.getProperty("uid");
        password=prop.getProperty("password");
        url="jdbc:oracle:thin:@"+host+":"+port+":"+sid;
    }
    public JDBCServer() throws SQLException
    {
        this.conn=DriverManager.getConnection(url,uid,password);      
        
    }
    public void beginTrans()
    {
        if(!this.inTrans)
        {
            try
            {
                this.conn.setAutoCommit(false);
                this.inTrans=true;
            }
            catch (SQLException ex)
            {
                ExceptionManager.handlerException(ex);
            }
        }
    }
    public void commitTrans()
    {
        if(this.inTrans)
        {
            try
            {
                this.conn.commit();
                this.inTrans=false;
            }
            catch (SQLException ex)
            {
                ExceptionManager.handlerException(ex);
            }
        }
    }
    public void rollbackTrans()
    {
        if(this.inTrans)
        {
            try
            {
                this.conn.rollback();
                this.inTrans=false;
            }
            catch (SQLException ex)
            {
                ExceptionManager.handlerException(ex);
            }
        }
    }
    public java.sql.Statement getStatement() throws SQLException
    {
        Statement stmt=this.conn.createStatement();
        return stmt;
    }
    public java.sql.CallableStatement getCallableStatement(String sp) throws SQLException
    {
        java.sql.CallableStatement  csmt= this.conn.prepareCall("{call "+sp+"}");
        return csmt;
    }
    public java.sql.PreparedStatement getPreparedStatement(String sql) throws SQLException
    {
        java.sql.PreparedStatement psmt=this.conn.prepareStatement(sql);
        return psmt;
    }
    public void closeConnection()
    {
        try
        {
            if(this.conn!=null)
            {
                this.conn.close();
            }
        }
        catch (Exception ex)
        {
             ExceptionManager.handlerException(ex);
        }
    }
    public ResultSet execQuery(String sql)throws SQLException
    {
        Statement stmt=this.getStatement();
        ResultSet rs=stmt.executeQuery(sql);
        return rs;
    }
    public boolean execInsert(String sql)throws SQLException
    {
        Statement stmt=this.getStatement();
        int count=stmt.executeUpdate(sql);
        this.closeConnection();
        return (count==1);
    }
    public int exceUpdate(String sql)throws SQLException
    {
        Statement stmt=this.getStatement();
        int count=stmt.executeUpdate(sql);
        this.closeConnection();
        return  count;
    }
    public int execDelete(String sql)throws SQLException
    {
        Statement stmt=this.getStatement();
        int count=stmt.executeUpdate(sql);
        this.closeConnection();
        return  count;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -