所以错误如下:
Error in Database Connection: Error getting connection to database - java.sql.SQLException: No suitable driver found for jdbc:sqlserver://isd.ktu.lt:1433;DatabaseName=LN2012_bakDB2
涉及方法的类看起来像这样:
package Core;
import DataTypes.Parameters;
import Interfaces.OutputInterface;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.pool.impl.GenericObjectPool;
/**
*
* @author arturas
*/
public class DatabaseConnection {
String specificError = "Error in Database Connection: ";
OutputInterface gui = null;
boolean allowOutput = true;
GenericObjectPool connectionPool;
ConnectionFactory connectionFactory;
PoolableConnectionFactory poolableConnectionFactory;
PoolingDriver driver;
Connection con = null;
public DatabaseConnection(Parameters params) {
// parameters and the output
this.gui = params.getGui();
// activate database pool
connectionPool = new GenericObjectPool(null);
connectionFactory = new DriverManagerConnectionFactory(params.getDbAdr(), params.getDbUser(), params.getDbPass());
poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
driver = new PoolingDriver();
driver.registerPool("GenTreeDatabase", connectionPool);
}
public void openConn() {
if (allowOutput) gui.print("Getting connection to database");
try {
con = DriverManager.getConnection("jdbc:apache:commons:dbcp:GenTreeDatabase");
if (con != null) {
if (allowOutput) gui.print("Connection to database was successful");
}
} catch (SQLException ex) {
gui.err(specificError + "Error getting connection to database - " + ex);
}
}
public void closeConn() {
try {
con.close();
if (allowOutput) {
gui.print("Connection to database closed successfully");
}
} catch (SQLException ex) {
gui.err(specificError + ex);
}
}
调用try in方法openConn时出现错误。 有人可以帮我吗?
答案 0 :(得分:1)
您收到此错误,因为类路径中没有驱动程序。可能在您的桌面应用程序中有。您需要将驱动程序的.jar
文件放入servlet容器的全局类路径或应用程序类路径中,它应该可以工作。
我更喜欢将驱动程序的jar添加到服务器全局类路径中,因为可能有多个应用程序将使用相同的.jar
文件来加载驱动程序。
答案 1 :(得分:0)
确保这个
1)你应该确保.jar库与你正在使用的RDMS兼容 2)您在
中包含.jar用于连接netbeansprojectproperties-->libraries
3)将.jar复制到
C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.26\lib
这很重要
如果你没有位置的驱动程序你没有找到错误但是 你不合适所以我认为版本必须是不兼容的,所以你使用的是什么版本的sql server ...