Simple JDBC Application for Java DB

Source: Internet
Author: User

Today, download the jdk1.6.0, and then slowly to learn 1.6 new features and some of the classic examples. Let's take a look at this minimalist example of Java DB:
Simple JDBC Application (source Simpleapp.java, Documents and derby.jar,derbynet.jar,derbyclient.ar files, please find them from jdk1.6.0)

This example is a minimally-limited JDBC application. About this program:

    • To run within a nested mode (default) or as a client in a server environment, depending on the parameters passed to the program
    • Start the Derby engine if you are running in in-block mode
    • If you are running in client mode, connect to the Derby network server
    • Create and connect to a database
    • Create a table
    • Inserting data
    • Update data
    • Querying data
    • Delete Table
    • Close connection
    • If you are running in nested mode, turn off Derby.

The following is the source code:

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.Properties;
    * * @author Janet */public class Simpleapp {/* The default mode is the inline/public String framework = "embedded";
    Public String Driver = "Org.apache.derby.jdbc.EmbeddedDriver";
    Public String protocol = "Jdbc:derby:";
    public static void Main (string[] args) {new Simpleapp (). Go (args);
        } void Go (string[] args) {/* processing parameters to determine whether this program is used as inline or as a client/parsearguments (args);
        System.out.println ("Simpleapp starting in" + Framework + "mode.");
             try {/* load driver, if inline mode, this will start derby because it is not yet running.
            * * Class.forName (Driver). newinstance ();
            System.out.println ("Loaded the appropriate driver.");
            Connection conn = null;
            Properties Props = new properties (); Props.put ("User", "User1 ");
            Props.put ("Password", "user1");
            Create=true will create the database Derbydb conn = drivermanager.getconnection (protocol + "Derbydb;create=true", props);
            System.out.println ("Connected to and created Database Derbydb");
            Conn.setautocommit (false);//Set autocommit mode Statement s = conn.createstatement ();
             /* Create a table, add a few records and update one.
            * * S.execute ("CREATE TABLE derbydb (num int, addr varchar (40))");
            SYSTEM.OUT.PRINTLN ("Created table Derbydb");
            S.execute ("INSERT Into Derbydb values (1956, ' Webster St. ')");
            System.out.println ("Inserted 1956 Webster");
            S.execute ("INSERT into derbydb values (1910, ' Union St. ')");
            System.out.println ("Inserted 1910 Union");
            S.execute ("Update derbydb set num=180, addr= ' Grand Ave. ' Where num=1956");
            System.out.println ("Updated 1956 Webster to 180 grand"); S.exeCute ("Update derbydb set num=300, addr= ' Lakeshore Ave. ' Where num=180");
            System.out.println ("Updated 180 Grand to Lakeshore");
             /* Query and verify the results.
            * * ResultSet rs = s.executequery ("Select num, addr from derbydb order by num");
            if (!rs.next ()) {throw new Exception ("Wrong number of rows");
            } if (Rs.getint (1)!=) {throw new Exception ("Wrong Row returned");
            } if (!rs.next ()) {throw new Exception ("Wrong number of rows");
            } if (Rs.getint (1)!= 1910) {throw new Exception ("Wrong Row returned");
            } if (Rs.next ()) {throw new Exception ("Wrong number of rows");
            } System.out.println ("verified the Rows"); S.execute ("drop table Derbydb ");//delete tables System.out.println (" Dropped table Derbydb ");
            Rs.close ();
            S.close ();
            System.out.println ("Closed result set and statement");
            Conn.commit ();
            Conn.close ();
           
            SYSTEM.OUT.PRINTLN ("Committed transaction and closed connection");
            Boolean gotsqlexc = false; if (Framework.equals ("embedded")) {try {drivermanager.ge
                Tconnection ("Jdbc:derby:;shutdown=true");/close Database service} catch (SQLException se)
                {Gotsqlexc = true; } if (!gotsqlexc) {System.out.println ("Database did not shut down nor
                Mally ");
                else {System.out.println ("Database shut down normally");
 }
            }       catch (Throwable e) {System.out.println ("Exception thrown:");
            if (e instanceof SQLException) {printsqlerror ((SQLException) e);
            else {e.printstacktrace ();
    } System.out.println ("Simpleapp finished"); } static void Printsqlerror (SQLException e) {while (e!= null) {SYSTEM.OUT.PRINTLN (E)
            . toString ());
        E = E.getnextexception ();
        } private void Parsearguments (string[] args) {
//System.setproperty ("Derby.system.home", "c:\\dbdata");//So you can set up a directory of database data

int length = Args.length; for (int index = 0; index < length; index++) {if (Args[index].equalsignorecase ("Jccjdbcclient")) {framework = "JCCJDBC"; Driver = "Com.ibm.db2.jcc.DB2Driver"; protocol = "jdbc:derby:net://localhost:1527/"; } if (Args[index].equalsignorecase ("Derbyclient")) {framework = "derbyclient"; Driver = "Org.apache.derby.jdbc.ClientDriver"; protocol = "jdbc:derby://localhost:1527/"; } } } }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.