HelloJavaDB


public class HelloJavaDB {
  public static void main(String[] args) {
    try { // load the driver
      Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
      System.out.println("Load the embedded driver");
      Connection conn = null;
      Properties props = new Properties();
      props.put("user", "user1"); 
      props.put("password", "user1");       //create and connect the database named helloDB       conn=DriverManager.getConnection("jdbc:derby:helloDB;create=true", props);       System.out.println("create and connect to helloDB");       conn.setAutoCommit(false);       // create a table and insert two records       Statement s = conn.createStatement();       s.execute("create table hellotable(name varchar(40), score int)");       System.out.println("Created table hellotable");       s.execute("insert into hellotable values('Ruth Cao', 86)");       s.execute("insert into hellotable values ('Flora Shi', 92)");       // list the two records       ResultSet rs = s.executeQuery(         "SELECT name, score FROM hellotable ORDER BY score");       System.out.println("namettscore");       while(rs.next()) {         StringBuilder builder = new StringBuilder(rs.getString(1));         builder.append("t");         builder.append(rs.getInt(2));         System.out.println(builder.toString());       }       // delete the table       s.execute("drop table hellotable");       System.out.println("Dropped table hellotable");       rs.close();       s.close();       System.out.println("Closed result set and statement");       conn.commit();       conn.close();       System.out.println("Committed transaction and closed connection");       try {
        // perform a clean shutdown         DriverManager.getConnection("jdbc:derby:;shutdown=true");       } catch (SQLException se) {         System.out.println("Database shut down normally");       }     } catch (Throwable e) {       // handle the exception     }     System.out.println("SimpleApp finished");   } }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值