我们公司常用的数据库连接类,或许对你有些帮助。DbConnect.java:package com.web.db;import java.io.File;
import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;public class DbConnect
{ private static DbConnect instance = null;
private static DataSource ds = null;
private static String dsName = null;
private static String username = null;
private static String password = null; public DbConnect()
{
} public static DbConnect getInstance()
{
if(instance == null)
instance = new DbConnect();
return instance;
} public void loadCfgFile()
throws Exception
{
String cfgFileName = null;
try
{
cfgFileName = System.getProperties().getProperty("CONFIG_FILE","c:/boot.properties");
File cfgFile = new File(cfgFileName);
FileInputStream in = new FileInputStream(cfgFile);
Properties p = new Properties();
p.load(in);
dsName = p.getProperty("system.datasource.name");
username = p.getProperty("system.database.username");
password = p.getProperty("system.database.password");
in.close();
}
catch(Exception ex)
{
throw new Exception(String.valueOf(String.valueOf((new StringBuffer("Load from configfile(")).append(cfgFileName).append(") error . ").append(ex.getMessage()))));
}
} public Connection getConnection(String url, String name, String password, int timeout)
throws Exception
{
Driver driver = (Driver)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
DriverManager.registerDriver(driver);
if(timeout > 0)
DriverManager.setLoginTimeout(timeout);
Connection conn = DriverManager.getConnection(url, name, password);
return conn;
} public Connection getConnection(String dsName, String name, String password)
throws Exception
{
Connection conn = null;
if(ds == null)
try
{
Context ctx = new InitialContext();
ds = (DataSource)ctx.lookup(dsName);
}
catch(Exception e)
{
throw new Exception(" can't get db connection , result : ".concat(String.valueOf(String.valueOf(e.getMessage()))));
}
try
{
if(username == null || username.equals(""))
conn = ds.getConnection();
else
conn = ds.getConnection(name, password);
}
catch(Exception e)
{
throw new Exception(" can't get db connection , result : ".concat(String.valueOf(String.valueOf(e.getMessage()))));
}
return conn;
} public Connection getConnection()
throws Exception
{
if(dsName == null)
loadCfgFile();
return getConnection(dsName, username, password);
}}
import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;public class DbConnect
{ private static DbConnect instance = null;
private static DataSource ds = null;
private static String dsName = null;
private static String username = null;
private static String password = null; public DbConnect()
{
} public static DbConnect getInstance()
{
if(instance == null)
instance = new DbConnect();
return instance;
} public void loadCfgFile()
throws Exception
{
String cfgFileName = null;
try
{
cfgFileName = System.getProperties().getProperty("CONFIG_FILE","c:/boot.properties");
File cfgFile = new File(cfgFileName);
FileInputStream in = new FileInputStream(cfgFile);
Properties p = new Properties();
p.load(in);
dsName = p.getProperty("system.datasource.name");
username = p.getProperty("system.database.username");
password = p.getProperty("system.database.password");
in.close();
}
catch(Exception ex)
{
throw new Exception(String.valueOf(String.valueOf((new StringBuffer("Load from configfile(")).append(cfgFileName).append(") error . ").append(ex.getMessage()))));
}
} public Connection getConnection(String url, String name, String password, int timeout)
throws Exception
{
Driver driver = (Driver)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
DriverManager.registerDriver(driver);
if(timeout > 0)
DriverManager.setLoginTimeout(timeout);
Connection conn = DriverManager.getConnection(url, name, password);
return conn;
} public Connection getConnection(String dsName, String name, String password)
throws Exception
{
Connection conn = null;
if(ds == null)
try
{
Context ctx = new InitialContext();
ds = (DataSource)ctx.lookup(dsName);
}
catch(Exception e)
{
throw new Exception(" can't get db connection , result : ".concat(String.valueOf(String.valueOf(e.getMessage()))));
}
try
{
if(username == null || username.equals(""))
conn = ds.getConnection();
else
conn = ds.getConnection(name, password);
}
catch(Exception e)
{
throw new Exception(" can't get db connection , result : ".concat(String.valueOf(String.valueOf(e.getMessage()))));
}
return conn;
} public Connection getConnection()
throws Exception
{
if(dsName == null)
loadCfgFile();
return getConnection(dsName, username, password);
}}
public class TestBean{
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");//如果是Ms Sql的就换成相应的就行
String url = "jdbc:oracle:thin:@localhost:1521:数据库名称";
String user = "xxx";
String password = "xxx";
Connection conn = DriverManager.getConnection(url, user, password);
Connection con=conn.getConn();
Statement stmt = con.createStatement();
String sql = "select ........ ";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println("first:"+rs.getString(1));
System.out.println("second:"+rs.getString(2));
........
}
rs.close();
stmt.close();
con.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.println("连接失败!");
} }
}
DataSource ds=(DataSource) ctx.lookup("jndiname");
Connection conn=ds.getConnection();的方法找到数据源可是为什么就是不行呢、、????