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)  (Click here for the information) (For NetBeans) -Check the appropriate setting method for JDBC driver, etc. ・ 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.



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;
        //Step 1 Driver registration

        //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 ({
            System.out.println("EMP_NO = " + rs.getString(1));
            System.out.println("EMP_NAME = " + rs.getString(2));
        } catch(ClassNotFoundException e){
        } catch(SQLException e){
        //Step 7:Exit JDBC object
        } finally{
            if(rs != null) rs.close();
            if(stmt != null) stmt.close();
            if(con != null) con.close();
          }catch (SQLException e){}

Example 2 Execution result

EMP_NO = 1

EMP_NO = 2

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