简体   繁体   中英

Connecting to Derby using JDBC

I am trying to connect to Derby database on my locahost using JDBC.

I have started the database using the command: java -jar lib;derbyrun.jar server start , which starts successfully on port 1527.

On another command terminal, I use the command: java -classpath .;lib;derbyclient.jar testsqldatabase.TestSQLDatabase but I get the error below:

java.sql.SQLException: No suitable driver found for jdbc:postgresql:COREJAVA
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at testsqldatabase.TestSQLDatabase.getConnection(TestSQLDatabase.jav
)
        at testsqldatabase.TestSQLDatabase.runTest(TestSQLDatabase.java:39)
        at testsqldatabase.TestSQLDatabase.main(TestSQLDatabase.java:26)

My datatbase.properties file contains the following lines:

jdbc.drivers=org.postgresql.Driver
jdbc.url=jdbc:postgresql:COREJAVA
jdbc.username=dbuser
jdbc.password=secret

The java program is is listed below:

public class TestSQLDatabase 
{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException
    {
        try
        {
            runTest();
        }
        catch(SQLException ex)
        {
            for(Throwable t: ex)
                t.printStackTrace();
        }
    }
    /*Runs a test by creating a table, adding a value, 
    showing the table contents, removing the table*/

    public static void runTest() throws SQLException, IOException
    {
        try(Connection conn = getConnection())
        {
            Statement stat = conn.createStatement();

            stat.executeUpdate("CTEATE TABLE Greetings (Message CHAR(20))");
            stat.executeUpdate("INSERT INTO Greetings VALUES ('Hello, World!')");

            try(ResultSet result = stat.executeQuery("SELECT * FROM Greetings"))
            {
                if(result.next())
                    System.out.println(result.getString(1));
            }
            stat.executeUpdate("DROP TABLE Greetings");
        }
    }

    /*
     * Gets a connection from the properties specified in the 
     * file database.properties. @return the database connection
     */
    public static Connection getConnection() throws SQLException, IOException
    {
        Properties props = new Properties();
        try(InputStream in = Files.newInputStream(Paths.get("database.properties")))
        {
            props.load(in);
        }
        String drivers = props.getProperty("jdbc.drivers");
        if(drivers != null) System.setProperty("jdbc.drivers", drivers);
        String url = props.getProperty("jdbc.url");
        String username = props.getProperty("jdbc.username");
        String password = props.getProperty("jdbc.password");

        return DriverManager.getConnection(url, username, password);
    }
}

Can anyone figure out why I am getting that error from the second command terminal?

Thanks, much appreciated

Derby is a database. PostgreSQL is a different database. You're running a Derby database and you need the corresponding Derby JDBC driver to talk to it - not the PostgreSQL one.

You want to connect to Derby by using the PostgreSQL driver (in the properties file); also the URL of the database is no written well; it should be: jdbc:${dataBaseVendor}:${server}:${port}/${databaseName} where in your case databaseVendor=derby.

And also make sure you have the Derby JDBC driver jar on your classpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM