📄 dbhelper.java
字号:
package com.gctech.sms.voice.common;
import java.util.*;
import java.io.*;
import javax.sql.*;
import java.sql.*;
import javax.naming.*;
import com.gctech.sms.voice.*;
/**
*
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: ted</p>
* @author li jia zhi
* @version 1.0
*/
public class DBHelper
{
public static boolean isDebug = false;
public static DataSource ds = null;
static Context context = null;
public static Connection getConn() throws VoiceException
{
if(isDebug)
{
return getDebugConnection();
}
try
{
return getDataSource().getConnection();
}
catch (SQLException ex)
{
throw new VoiceException("得到connection pool错误"+ex.getMessage());
}
catch(VoiceException ure)
{
throw ure;
}
}
public static Connection get116Conn() throws VoiceException
{
return get116DebugConnection();
// if(isDebug)
// {
// return get116DebugConnection();
// }
// try
// {
// return get116DataSource().getConnection();
// }
// catch (SQLException ex)
// {
// throw new VoiceException("得到connection pool错误"+ex.getMessage());
// }
// catch(VoiceException ure)
// {
// throw ure;
// }
}
public static void cleanup(Connection conn)
{
try
{
if(conn==null)
{
return;
}
else
{
conn.close();
}
}
catch (Exception ex)
{
}
}
private static DataSource getDataSource() throws VoiceException
{
try
{
if(ds==null)
{
Context context = getInitialContext();
ds = (DataSource)context.lookup("VoxSMSDataSource");
}
return ds;
}
catch(VoiceException ure)
{
throw ure;
}
catch (Exception ex)
{
throw new VoiceException("不能得到数据源"+ex.getMessage());
}
}
private static DataSource get116DataSource() throws VoiceException
{
try
{
if(ds==null)
{
Context context = getInitialContext();
ds = (DataSource)context.lookup("VoxSMS116DataSource");
}
return ds;
}
catch(VoiceException ure)
{
throw ure;
}
catch (Exception ex)
{
throw new VoiceException("不能得到数据源"+ex.getMessage());
}
}
/**
* 得到context
* @return
* @throws VoiceException
* @todo 验证线程安全
*/
public static Context getInitialContext() throws VoiceException
{
try
{
return context = new InitialContext();
//return new InitialContext(environment);
}
catch (Exception ex) {
throw new VoiceException("得到context失败"+ ex.getMessage());
}
// return context;
}
public static Connection getDebugConnection() throws VoiceException
{
Connection con = null;
//getting the properties
// String driver = PropertyLoader.getInstance().getProperty("DataBase.JDBCDriver");
// if(driver == null)
// throw new VoiceException("Missing property 'DataBase.JDBCDriver' in SourceGenerator.properties file");
// String dburl = PropertyLoader.getInstance().getProperty("DataBase.url");
// if(dburl == null)
// throw new VoiceException("Missing property 'DataBase.url' in SourceGenerator.properties file");
// String userid = PropertyLoader.getInstance().getProperty("DataBase.userid");
// if(userid == null)
// throw new VoiceException("Missing property 'DataBase.userid' property in SourceGenerator.properties file");
// String password = PropertyLoader.getInstance().getProperty("DataBase.password");
// if(password==null)
// throw new VoiceException("Missing property 'DataBase.password' property in SourceGenerator.properties file");
try
{
//creating connection
Class.forName("oracle.jdbc.driver.OracleDriver");
String dburl = "jdbc:oracle:thin:@10.1.2.130:1521:ninecard";
String userid = "voicesms";
String password = "voicesms";
con = DriverManager.getConnection(dburl,userid,password);
if(con == null)
throw new VoiceException("Error in creating DBConnection. Make sure your DBAccess properties are correct." +
"if correct there may be other problem in Accessing given Database ");
}
catch(Exception cnf)
{
cnf.printStackTrace();
throw new VoiceException(cnf.getMessage());
}
return con;
}
public static Connection get116DebugConnection() throws VoiceException
{
Connection con = null;
//getting the properties
String driver = "oracle.jdbc.driver.OracleDriver";
try {
//creating connection
Class.forName(driver);
String dburl="jdbc:oracle:thin:@10.168.10.3:1521:bjoradb";
String userid="ipswitch";
String password="suntek";
con = DriverManager.getConnection(dburl,userid,password);
if(con == null)
throw new VoiceException("Error in creating DBConnection. Make sure your DBAccess properties are correct." +
"if correct there may be other problem in Accessing given Database ");
}catch(Exception cnf ) {
cnf.printStackTrace();
throw new VoiceException(cnf.getMessage());
}
return con;
}
public static void cleanupPs(PreparedStatement ps)
{
try
{
if(ps != null)
ps.close();
}
catch(SQLException sqle)
{
//ignore;
}
}
public static int nextVoiceSeqId(Connection conn)
throws SQLException
{
int seqId = 0;
try
{
String sql = "select voice_seq.nextval from dual";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
seqId = rs.getInt(1);
}
else
{
throw new SQLException("找不到指定序列值");
}
rs.close();
}
catch(SQLException ex)
{
}
return seqId;
}
public static int nextUserSeqId(Connection conn)
throws SQLException
{
int seqId = 0;
try
{
String sql = "select user_seq.nextval from dual";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
seqId = rs.getInt(1);
}
else
{
throw new SQLException("找不到指定序列值");
}
rs.close();
}
catch(SQLException ex)
{
}
return seqId;
}
public static int nextHistorySeqId(Connection conn)
throws SQLException
{
int seqId = 0;
try
{
String sql = "select history_seq.nextval from dual";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
seqId = rs.getInt(1);
}
else
{
throw new SQLException("找不到指定序列值");
}
rs.close();
}
catch(SQLException ex)
{
}
return seqId;
}
public static void main(String[] args) throws Exception
{
//Connection conn = new EJBUtil().getConnection();
DBHelper.isDebug = true ;
DBHelper util = new DBHelper();
Connection conn = util.getConn();
// PreparedStatement ps = conn.prepareStatement("select * from t_nm_record");
// ResultSet rs = ps.executeQuery();
// while(rs.next())
// {
// System.out.println(rs.getInt(1));
// }
}
}
class PropertyLoader
{
static PropertyLoader l = null;
static String path = System.getProperty("user.home")+"/DaoTool.txt";
Properties p = null;
private PropertyLoader()
{
load();
}
public static PropertyLoader getInstance()
{
if(l==null)
{
l = new PropertyLoader();
}
return l;
}
private void load()
{
try
{
p =new Properties();
p.load(new FileInputStream(new File(path)));
}
catch (Exception ex)
{
ex.printStackTrace();
// Logger.info(ex.getMessage());
}
}
public String getProperty(String key)
{
return (String)p.get(key);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -