Search code examples
javasql-serverjdbcjtds

JTDS not connecting to correct database


I am using JTDS to connect to a MS SQL Server. Connection to the database is no problem, but when I try to execute a statement, I get a Database 'java' does not exist exception.

ConnectionString:

conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost;DatabaseName=MyDatabase;user=testuser;password=testpassword");

Trying to execute the script:

private void runStatement(String scriptLocation) {
    if(scriptLocation == null) {
        return;
    }

    try {
        InputStream is = getClass().getClassLoader().getResourceAsStream(scriptLocation);
        String query = is.toString();
        is.close();

        Statement stmt = conn.createStatement();
        stmt.executeQuery(query);
    } catch(IOException | SQLException ex) {
        log.warning(ex.getMessage());
    }
}

Stacktrace:

WARNING: Database 'java' does not exist. Make sure that the name is entered correctly.
java.sql.SQLException: Database 'java' does not exist. Make sure that the name is entered correctly.
    at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372)
    at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2988)
    at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2421)
    at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:671)
    at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQLQuery(JtdsStatement.java:505)
    at net.sourceforge.jtds.jdbc.JtdsStatement.executeQuery(JtdsStatement.java:1427)
    at com.exampe.MyJTDSConnection.runStatement(MyJTDSConnection.java:238)
    at com.exampe.MyJTDSConnection.loadPageTitle(MyJTDSConnection.java:208)
    at com.exampe.MyJTDSConnection.runTesting(MyJTDSConnection.java:69)
    at com.exampe.SeleniumTesting.runTest(SeleniumTesting.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    ...

Solution

  • As mentioned in the comment to your question, applying the .toString() method to the InputStream object does not read the InputStream. Instead it just returns a String representation of the object itself, not what the object contains.

    For example, my Java project has a resource file named "script.sql" that contains:

    SELECT @@VERSION
    

    The following code compares the result of simply doing a .toString() on the object vs. using Apache Commons IO to actually read the InputStream into a String:

    package resourceTest;
    
    import java.io.InputStream;
    import org.apache.commons.io.IOUtils;
    
    public class ResourceTestMain {
    
        public static void main(String[] args) {
            try (InputStream is = ResourceTestMain.class.getClassLoader().getResourceAsStream("resources/script.sql")) {
                String toStringValue = is.toString();
                String contents = IOUtils.toString(is, "UTF-8");
                is.close();
                System.out.println("is.toString() returned:");
                System.out.println("    " + toStringValue);
                System.out.println();
                System.out.println("IOUtils.toString(is, \"UTF-8\") returned:");
                System.out.println("    " + contents);
            } catch (Exception e) {
                e.printStackTrace(System.out);
            }
        }
    
    }
    

    The results are:

    is.toString() returned:
        java.io.BufferedInputStream@804a77
    
    IOUtils.toString(is, "UTF-8") returned:
        SELECT @@VERSION