好的
我正在尝试使用Apache连接池连接到一些不同的数据库,但是我很难通过Jtds使用LocalDb。我收到以下错误消息:
Aug 31, 2018 8:49:24 PM Model.DataAccessLayer.Database.ConnectionPool_LocalDb main
SEVERE: null
java.sql.SQLException: Network error IOException: \\.\pipe\MSSQL$LOCALDB#F1954C00\sql\query (The system cannot find the file specified)
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:436)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at org.apache.commons.dbcp2.DriverManagerConnectionFactory.createConnection(DriverManagerConnectionFactory.java:85)
at org.apache.commons.dbcp2.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:260)
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:889)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:433)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:362)
at org.apache.commons.dbcp2.PoolingDriver.connect(PoolingDriver.java:128)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at Model.DataAccessLayer.Database.ConnectionPool_LocalDb.getConnection(ConnectionPool_LocalDb.java:175)
at Model.DataAccessLayer.Database.ConnectionPool_LocalDb.main(ConnectionPool_LocalDb.java:100)
Caused by: java.io.FileNotFoundException: \\.\pipe\MSSQL$LOCALDB#F1954C00\sql\query (The system cannot find the file specified)
at java.io.RandomAccessFile.open0(Native Method)
at java.io.RandomAccessFile.open(RandomAccessFile.java:316)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:243)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:124)
at net.sourceforge.jtds.jdbc.SharedLocalNamedPipe.<init>(SharedLocalNamedPipe.java:64)
at net.sourceforge.jtds.jdbc.JtdsConnection.createNamedPipe(JtdsConnection.java:518)
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:328)
我正在使用的代码看起来像这样。如您所见,我已经尝试了一些不同的连接字符串,因为我觉得这就是问题所在。我:
public static void main(String[] args) {
//Connection Pool
ConnectionPool_LocalDb pool = null;
String driverPoolName = "docs";
try {
//Initialise H2 Connection
String dbConnStrH2 = "jdbc:h2:~/test";
String sqlH2 = "SELECT * FROM TEST2;";
//Initialise Connection Pool
pool = new ConnectionPool_LocalDb("jdbc:apache:commons:dbcp:", "org.apache.commons.dbcp2.PoolingDriver");
//H2 Database
pool.setupDriver(dbConnStrH2, driverPoolName);
Connection h2Connection = pool.getConnection(driverPoolName);
ResultSet h2ResultSet = pool.getResultSet(h2Connection, sqlH2);
System.out.println("h2ResultSet = " + pool.getResultSetAsString(h2ResultSet));
System.out.println("poolStats = " + pool.getPrintPoolStats(driverPoolName));
//Initialise LocalDb Connection
String driverPoolNameLocalDb = "localDb";
// String dbConnStrLocalDb = "jdbc:jtds:sqlserver://./instance=LOCALDB#F1954C00;namedPipe=true";
// String dbConnStrLocalDb = "jdbc:jtds:sqlserver://./instance=LOCALDB#F1954C00;namedPipe=true";
// String dbConnStrLocalDb = "jdbc:jtds:sqlserver://./;instance=LOCALDB#F1954C00;namedPipe=true";
// String dbConnStrLocalDb = "jdbc:jtds:sqlserver://./docs;instance=LOCALDB#F1954C00;namedPipe=true;domain=WORKGROUP";
String dbConnStrLocalDb = "jdbc:jtds:sqlserver://./;instance=LOCALDB#F1954C00;namedPipe=true;domain=WORKGROUP";
String sqlLocalDb = "SELECT * FROM [docs].[dbo].[documents]";
//Sql Server LocalDb
pool.setupDriver(dbConnStrLocalDb, driverPoolName);
Connection LocalDbConnection = pool.getConnection(driverPoolName);
ResultSet LocalDbResultSet = pool.getResultSet(LocalDbConnection, sqlLocalDb);
System.out.println("LocalDbResultSet = " + pool.getResultSetAsString(LocalDbResultSet));
System.out.println("poolStats = " + pool.getPrintPoolStats(driverPoolNameLocalDb));
//Shutting down connections
h2Connection.close();
LocalDbConnection.close();
pool.shutdownDriver(driverPoolName);
} catch (Exception ex) {
Logger.getLogger(ConnectionPool_LocalDb.class.getName()).log(Level.SEVERE, null, ex);
}
//Shut down driver pools
try {
pool.shutdownDriver(driverPoolName);
} catch (Exception e) {
e.printStackTrace();
}
}
也许我可能在以下方面遭受与阿杰相同的问题: Connecting to SQL Server LocalDB using JDBC
错误发生在Connection LocalDbConnection = pool.getConnection(driverPoolName);
行
令人讨厌的是,通过jtds连接到生产SQL Server确实很容易,但遗憾的是我在开发阶段无法这样做。
我已经设法通过Sql Server Management Studio连接到LocalDb。
任何帮助将不胜感激。