Connect to Java DB (Derby) with org.apache.derby.jdbc.EmbeddedDriver : Java DB Derby « Database « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Database » Java DB Derby 
20. 37. 17. Connect to Java DB (Derby) with org.apache.derby.jdbc.EmbeddedDriver
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class JavaDBDemo {
  static Connection conn;

  public static void main(String[] args) {
    String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    String connectionURL = "jdbc:derby:myDatabase;create=true";
    String createString = "CREATE TABLE Employee (NAME VARCHAR(32) NOT NULL, ADDRESS VARCHAR(50) NOT NULL)";
    try {
      Class.forName(driver);
    catch (java.lang.ClassNotFoundException e) {
      e.printStackTrace();
    }
    try {
      conn = DriverManager.getConnection(connectionURL);
      Statement stmt = conn.createStatement();
      stmt.executeUpdate(createString);

      PreparedStatement psInsert = conn.prepareStatement("insert into Employee values (?,?)");

      psInsert.setString(1, args[0]);
      psInsert.setString(2, args[1]);

      psInsert.executeUpdate();

      Statement stmt2 = conn.createStatement();
      ResultSet rs = stmt2.executeQuery("select * from Employee");
      int num = 0;
      while (rs.next()) {
        System.out.println(++num + ": Name: " + rs.getString(1"\n Address" + rs.getString(2));
      }
      rs.close();
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}
20. 37. Java DB Derby
20. 37. 1. New to Java 6 is a lightweight database called Derby.
20. 37. 2. The JavaDB Directory Structure
20. 37. 3. The JavaDB Environments
20. 37. 4. JavaDB Drivers
20. 37. 5. JavaDB URLs
20. 37. 6. JavaDB Tools
20. 37. 7. You can use the server tool to start JavaDB in a network client environment.
20. 37. 8. Ping JavaDB.
20. 37. 9. To view run time information for JavaDB, you can use the following command:
20. 37. 10. To stop JavaDB, you can use the following command:
20. 37. 11. Using the ij Tool
20. 37. 12. You can use the connect command of ij to establish a connection with a database
20. 37. 13. Viewing and Switching Connections
20. 37. 14. Working with Tables
20. 37. 15. To view the structure of the Employee table
20. 37. 16. Using the sysinfo Tool
20. 37. 17. Connect to Java DB (Derby) with org.apache.derby.jdbc.EmbeddedDriver
20. 37. 18. Connect to Derby database
20. 37. 19. The SQL script for creating the Employee Table
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.