以下为几个连接数据java文件
----DBConnect.java-------------------------------------------------------------------------------------
---------------------------------------------------------------------------------
package cn.edu.buaa.nlsde.db;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class DBConnect {
Connection conn = null;
/**
 * 通过JDBC获取Statement
 * @param dbname
 * @return
 */
public Connection getJDBCConn(String dbname) {
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
conn =
DriverManager.getConnection(
"jdbc:jtds:sqlserver://localhost:1433/"
+ dbname
+ ";user=sa;password=mysql");
}
catch (Exception e) {
System.err.println(e);
}
return conn;
}
/**
 * 通过JDBC:ODBC获取Statement
 * @param dsname
 * @return
 */
public Connection getODBCConn(String dsname) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn =
DriverManager.getConnection(
"jdbc:odbc:" + dsname,
"sa",
"mysql");
}
catch (Exception e) {
System.out.println(e);
}
return conn;
}
/**
 * 通过Context获取Statement
 * 服务器数据库连接池
 * @param resname
 * @return
 * @throws Exception
 */
public Connection getPoolConn(String resname)
throws Exception {
Context initCtx = new InitialContext();
if (initCtx == null) {
throw new Exception("不能获取Context!");
}
DataSource ds =
(DataSource)initCtx.lookup("java:comp/env/" + resname);
conn = ds.getConnection();
return conn;
}
/**
 * 通过自定义DBConnectionManager获取Statement 
 * 自定义数据库连接池
 * @param resname
 * @return
 * @throws Exception
 */
public Connection getConnPoolConn(String resname)
throws Exception {
DBConnectionManager connMgr =
DBConnectionManager.getInstance();
conn = connMgr.getConnection(resname, 3000);
if (conn == null) {
System.out.println("不能获取数据库连接.");
return null;
}
return conn;
}
}
----------DBConnBean.java-----------------------------------------------------
-----------------------------------------------------------------------------
//连接数据库的Java Bean文件名DBConnBean.java
package cn.edu.buaa.nlsde.bean;
import java.sql.*;
public class DBConnBean {
public DBConnBean() {
}
//declare variable
private Connection conn = null;
ResultSet rs = null;
private String server = "127.0.0.1";
private String port = "3306";
private String db = "test";
private String user = "root";
private String pass = "password";
private String drivername = "com.mysql.jdbc.Driver";
private String URL =
"jdbc:mysql://"
+ server
+ ":"
+ port
+ "/"
+ db
+ "?user="
+ user
+ "&password="
+ pass;
public Connection getConn() { //get database connection
try {
Class.forName(drivername).newInstance();
conn = DriverManager.getConnection(URL);
}
catch (Exception e) {
e.printStackTrace();
}
return this.conn;
}
public void setServer(String str) { //set server name
server = str;
}
public void setPort(String str) { //set server port
port = str;
}
public void setDB(String str) { //set db name
db = str;
}
public void setUser(String str) { //set user name
user = str;
}
public void setPass(String str) { //set user name
pass = str;
}
public ResultSet executeSQL(String str) {
try {
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(str);
}
catch (Exception e) {
e.printStackTrace();
}
return this.rs;
}
}
--------PoolBean.java-------------------------------------------------------------------
----------------------------------------------------------------------------------------
package cn.edu.buaa.nlsde.bean;
//连接池
import java.io.Serializable;
import java.sql.*;
import java.util.*;
public class PoolBean implements java.io.Serializable {
private String strDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
private String strURL = "jdbc:odbc:EIMS";
private int iSize = 1;
private String strUserName = "sa";
private String strPassword = "mysql";
private ConnectionBean vConnectionBean = null;
private Vector vPool = null;
public PoolBean() {
}
public void setDriver(String strDriver) {
if (strDriver != null) {
this.strDriver = strDriver;
}
}
public String getDriver() {
return strDriver;
}
public void setURL(String strURL) {
if (strURL != null) {
this.strURL = strURL;
}
}
public String getURL() {
return strURL;
}
public void setSize(int iSize) {
if (iSize > 1) {
this.iSize = iSize;
}
}
public int getSize() {
return iSize;
}
public String getUserName() {
return strUserName;
}
public void setUserName(String strUserName) {
if (strUserName != null) {
this.strUserName = strUserName;
}
}
public void setPassword(String strPassword) {
if (strPassword != null) {
this.strPassword = strPassword;
}
}
public String getPassword() {
return strPassword;
}
public void setConnectionBean(ConnectionBean vConnectionBean) {
if (vConnectionBean != null) {
this.vConnectionBean = vConnectionBean;
}
}
public ConnectionBean getConnectionBean() throws Exception {
Connection vConnection = getConnection();
ConnectionBean vConnectionBean =
new ConnectionBean(vConnection);
vConnectionBean.setUseState(true);
return vConnectionBean;
}
private Connection createConnection() throws Exception {
Connection vConnection = null;
vConnection =
DriverManager.getConnection(
strURL,
strUserName,
strPassword);
return vConnection;
}
public synchronized void initializePool() throws Exception {
if (strDriver == null) {
throw new Exception("没有提供驱动程序名称!");
}
if (strURL == null) {
throw new Exception("没有提供URL!");
}
if (iSize < 1) {
throw new Exception("连接池大小小于1!");
}
try {
Class.forName(strDriver);
for (int iIndex = 0; iIndex < iSize; iIndex++) {
Connection vConnection = createConnection();
if (vConnection != null) {
ConnectionBean vConnectionBean =
new ConnectionBean(vConnection);
addConnection(vConnectionBean);
}
}
}
catch (Exception eException) {
System.err.println(eException.getMessage());
throw new Exception(eException.getMessage());
}
}
private void addConnection(ConnectionBean vConnectionBean) {
if (vPool == null) {
vPool = new Vector(iSize);
}
vPool.addElement(vConnectionBean);
}
public synchronized void releaseConnection(Connection vConnection) {
for (int iIndex = 0; iIndex < vPool.size(); iIndex++) {
ConnectionBean vConnectionBean =
(ConnectionBean)vPool.elementAt(iIndex);
if (vConnectionBean.getConnection() == vConnection) {
System.err.println("释放第" + iIndex + "个连接!");
vConnectionBean.setUseState(false);
break;
}
}
}
public synchronized Connection getConnection() throws Exception {
ConnectionBean vConnectionBean = null;
for (int iIndex = 0; iIndex < vPool.size(); iIndex++) {
vConnectionBean = (ConnectionBean)vPool.elementAt(iIndex);
if (vConnectionBean.getUseState() == false) {
vConnectionBean.setUseState(true);
Connection vConnection =
vConnectionBean.getConnection();
return vConnection;
}
}
try {
Connection vConnection = createConnection();
vConnectionBean = new ConnectionBean(vConnection);
vConnectionBean.setUseState(true);
vPool.addElement(vConnectionBean);
}
catch (Exception eException) {
System.err.println(eException.getMessage());
throw new Exception(eException.getMessage());
}
return vConnectionBean.getConnection();
}
public synchronized void emptyPool() {
for (int iIndex = 0; iIndex < vPool.size(); iIndex++) {
System.err.println("关闭第" + iIndex + "JDBC连接!");
ConnectionBean vConnectionBean =
(ConnectionBean)vPool.elementAt(iIndex);
if (vConnectionBean.getUseState() == false) {
vConnectionBean.close();
}
else {
try {
java.lang.Thread.sleep(20000);
vConnectionBean.close();
}
catch (InterruptedException interruptedException) {
System.err.println(
interruptedException.getMessage());
}
}
}
}
}

解决方案 »

  1.   

    我看了,web-inf\lib下有4个包jtds-1.1.jar、msbase.jar、mssqlserver.jar、msutil.jar
      

  2.   

    这是用连接池什么的连接数据库,我想问下第二个java文件DBConnBean.java中,这一段
    private Connection conn = null;
    ResultSet rs = null;
    private String server = "127.0.0.1";
    private String port = "3306";
    private String db = "test";
    private String user = "root";
    private String pass = "password";
    private String drivername = "com.mysql.jdbc.Driver";
    private String URL =
    "jdbc:mysql://"
    + server
    + ":"
    + port
    + "/"
    + db
    + "?user="
    + user
    + "&password="
    + pass;
    这是不是连接mysql数据库的方式?我是导入别人的项目,现在连接不上数据库,在找原因,所以有些东西还不是很懂,请大虾们指教
      

  3.   

    你DBConnBean.java里面的port,drivername和url都写错了,那是连接mysql数据库的驱动包和url书写方式,
    private String port = "1433";
    private String drivername = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    private String URL =
    "jdbc:microsoft:sqlserver://"
    + server
    + ":"
    + port
    + ";DatabaseName="
    + db;