Java 8

时间:2019-07-01 17:12:29

标签: java maven jdbc derby

我正在尝试使用Maven软件将数据从Java类Restaurants2推送到基于derby的SQL数据库。

想法是Java类将在Derby数据库“ MyDB”中向DB添加一些信息。

不幸的是,由于异常,我无法运行该程序(请参见下面的控制台信息)。
我正在MacOS Mojave上使用Eclipse 4.12.0。
有人知道怎么了吗? 干杯!

    Java class Restaurants2: 

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.ResultSetMetaData;



    public class Restaurants2
    {

    // jdbc:derby:/Users/ap/MyDB;create=true
    private static String dbURL = "jdbc:derby:/Users/ap/MyDB/;create=true;user=***;password=***";
    private static String tableName = " MYTABELLE2";
    // jdbc Connection
    private static Connection conn = null;
    private static Statement stmt = null;

    public static void main(String[] args)
    {
        createConnection();
        insertRestaurants(5, "LaVals", "Berkeley");
        selectRestaurants();
        shutdown();
    }

    private static void createConnection()
    {
        try
        {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
            //Get a connection
            conn = DriverManager.getConnection(dbURL); 
        }
        catch (Exception except)
        {
            except.printStackTrace();
        }
    }

    private static void insertRestaurants(int id, String restName, String cityName)
    {
        try
        {
            stmt = conn.createStatement();
            stmt.execute("insert into " + tableName + " values (" +
                    id + ",'" + restName + "','" + cityName +"')");
            stmt.close();
        }
        catch (SQLException sqlExcept)
        {
            sqlExcept.printStackTrace();
        }
    }

    private static void selectRestaurants()
    {
        try
        {
            stmt = conn.createStatement();
            ResultSet results = stmt.executeQuery("select * from " + tableName);
            ResultSetMetaData rsmd = results.getMetaData();
            int numberCols = rsmd.getColumnCount();
            for (int i=1; i<=numberCols; i++)
            {
                //print Column Names
                System.out.print(rsmd.getColumnLabel(i)+"\t\t");  
            }

            System.out.println("\n-------------------------------------------------");

            while(results.next())
            {
                int id = results.getInt(1);
                String restName = results.getString(2);
                String cityName = results.getString(3);
                System.out.println(id + "\t\t" + restName + "\t\t" + cityName);
            }
            results.close();
            stmt.close();
        }
        catch (SQLException sqlExcept)
        {
            sqlExcept.printStackTrace();
        }
    }

    private static void shutdown()
    {
        try
        {
            if (stmt != null)
            {
                stmt.close();
            }
            if (conn != null)
            {
                DriverManager.getConnection(dbURL + ";shutdown=true");
                conn.close();
            }           
        }
        catch (SQLException sqlExcept)
        {

        }

    }
}    

该程序无法运行,我从控制台获得以下信息:

java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at Restaurants2.createConnection(Restaurants2.java:40)
at Restaurants2.main(Restaurants2.java:30)
Exception in thread "main" java.lang.NullPointerException
at Restaurants2.insertRestaurants(Restaurants2.java:54)
at Restaurants2.main(Restaurants2.java:31)`   

这就是我对POM文件的依赖性:

<dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.14.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.10.1.1</version>
</dependency>

</dependencies>

1 个答案:

答案 0 :(得分:1)

<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.15.1.3</version>
  

Java版本的输出为1.8.0_212

从10.15.x版本开始,Derby需要Java 9或更高版本(参考:here)。

如果要继续使用Java 8,则必须将Derby降级到10.14.2.0版本。