简体   繁体   中英

Connect to DB (Derby)

I try to connect to the database with this code:

      try
    {
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        String url = "jdbc:derby://localhost:1527/sample";
        Connection con = DriverManager.getConnection(url);
        PreparedStatement pstmt=con.prepareStatement("insert into app.discount_code values('A',22)");
        pstmt.executeUpdate();
        con.close();
    }

    catch(Exception e)

    {
         System.out.println(e.getMessage());

    }

..but not result. What do I need to do to connect to the DB?

Sir, You are connecting to derby network server. So you need to firstly config the environment and start the network server. Please also check the jdbc driver jars are correct.

About how to config and start derby network server.Look at here. http://db.apache.org/derby/papers/DerbyTut/ns_intro.html#ns_config_env

Also you'd better put the close method in your finally block.

You'd better paste your error message, it wil be helpful to find your problem.

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