Java and Derby integration using JDBC (using NetBeans)

Before writing a Java class to use JDBC,

-Check the JDK version -Set environment variables (so that the Java app can recognize the JDBC driver)   https://www.agtech.co.jp/html/v11manuals/11.20/jdbc/jdbc-2-2.html  (Click here for the information)   https://netbeans.org/competition/win-with-netbeans/mysql-client_ja.html (For NetBeans) -Check the appropriate setting method for JDBC driver, etc.   http://www.ne.jp/asahi/hishidama/home/tech/java/javadb.html ・ If you search for Derby JDBC, you will find a powerful site. -The user ID and password of the Derby database must be set to "APP".

How to write Java class to use JDBC (example)

The following is an example of outputting data from the database. The following outputs data from a database that has a table like Table 1.

Table 1 EMPLOYEES

EMP_NO EMP_NAME
1 HOGE
2 HORII

Example 1 Writing a JDBCcon class

import java.sql.*;
 public class JDBCcon {
     public static void main (String[] args){      
        Statement stmt = null;
        ResultSet rs = null;
        Connection con = null;
        
        try{
        //Step 1 Driver registration
        Class.forName("org.apache.derby.jdbc.ClientDriver");

         
        //Step 2 Designate the database
        String url = "jdbc:derby://localhost:1527/EmployeeDB";
        
        //Step 3 Establish a connection to the database
        con = DriverManager.getConnection(url,"APP","APP");
        
        //Step 4 Create a statement
        stmt = con.createStatement();
        
        //Step 5:SQL execution
        String sql ="SELECT * FROM EMPLOYEES";
         
        rs = stmt.executeQuery(sql);
        
        //Step 6:Processing results
          while (rs.next()){
            System.out.println("EMP_NO = " + rs.getString(1));
            System.out.println("EMP_NAME = " + rs.getString(2));
            System.out.println();
          }
        
        } catch(ClassNotFoundException e){
          System.out.println(e.getMessage());
          e.printStackTrace();
        } catch(SQLException e){
          System.out.println(e.getMessage());
          e.printStackTrace();
          
        //Step 7:Exit JDBC object
        } finally{
          try{
            if(rs != null) rs.close();
            if(stmt != null) stmt.close();
            if(con != null) con.close();
          }catch (SQLException e){}
        }    
    }
}

Example 2 Execution result

run:
EMP_NO = 1
EMP_NAME = HOGE      

EMP_NO = 2
EMP_NAME = HORII     

Postscript: From JDBC 4.0 of JDK 1.6, it seems to automatically load the driver in the classpath. Thank you for pointing out.

Recommended Posts

Java and Derby integration using JDBC (using NetBeans)
[Java] Relationship between H2DB and JDBC
[About JDBC that connects Java and SQL]
Create API using Retrofit2, Okhttp3 and Gson (Java)
Install java and android-sdk on Mac using homebrew
Java and JavaScript
XXE and Java
Create a portfolio app using Java and Spring Boot
Convert JSON and YAML in Java (using Jackson and SnakeYAML)
[Java] [SQL Server] Connect to local SQL Server 2017 using JDBC for SQL Server
[Java] Sort the list using streams and lambda expressions
How to convert A to a and a to A using AND and OR in Java
Mutual conversion between Java objects and JSON using Moshi
[Java10] Be careful of using var and generics together
[Java] Development with multiple files using package and import
Sorting using java comparator
Java true and false
[Java] String comparison and && and ||
Using JDBC on Linux
Scraping practice using Java ②
Java --Serialization and Deserialization
Scraping practice using Java ①
timedatectl and Java TimeZone
[Java] Branch and repeat
[Java] Variables and types
java (classes and instances)
Try using Spring JDBC
[Java] Overload and override
JDBC, CP, JPA, ORM .. Organize those between Java and RDB
Implement Thread in Java and try using anonymous class, lambda
Problems with Dijkstra's algorithm using PriorityQueue and adjacency list (java)
Tips for using Salesforce SOAP and Bulk API in Java
Socket communication with a web browser using Java and JavaScript ②
Socket communication with a web browser using Java and JavaScript ①
A Simple CRUD Sample Using Java Servlet / JSP and MySQL