H2 Database - JDBC Connection - Tutorialspoint PDF
H2 Database - JDBC Connection - Tutorialspoint PDF
H2 Database - JDBC Connection - Tutorialspoint PDF
Advertisements
H2 is a JAVA database. We can interact with this database by using JDBC. In this chapter,
we will see how to create a JDBC connection with H2 database and the CRUD operations
with the H2 database.
Class.forName ("org.h2.Driver");
Statement st = conn.createStatement();
Stmt.executeUpdate("sql statement");
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
Step 5 − Closing a connection.
Accept
conn.close();
Learn more
Before moving on to create a full program, we need to add h2-1.4.192.jar file to
CLASSPATH. We can get this jar from the folder C:\Program Files (x86)\H2\bin.
Create Table
In this example, we will write a program for create table. Consider a table named
Registration having the following fields.
2 First Varchar(255) No No
3 Last Varchar(255) No No
4 Age Number No No
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
// Database credentials
static final String USER = "sa";
static final String PASS = "";
Save the above program into H2jdbcCreateDemo.java. Compile and execute the above
program by executing the following commands in the command prompt.
\>javac H2jdbcCreateDemo.java
\>java H2jdbcCreateDemo
Connecting to database...
Creating table in given database...
Created table in given database...
Goodbye!
After this execution, we can check the table created using the H2 SQL interface.
Insert Records
In this example, we will write a program for inserting records. Let us insert the following
records into the table Registration.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
// Database credentials
static final String USER = "sa";
static final String PASS = "";
stmt.executeUpdate(sql);
sql = "INSERT INTO Registration " + "VALUES (101, 'Mahnaz', 'Fatma', 25)"
stmt.executeUpdate(sql);
sql = "INSERT INTO Registration " + "VALUES (102, 'Zaid', 'Khan', 30)"
stmt.executeUpdate(sql);
sql = "INSERT INTO Registration " + "VALUES(103, 'Sumit', 'Mittal', 28)"
stmt.executeUpdate(sql);
System.out.println("Inserted records into the table...");
Save the above program into H2jdbcInsertDemo.java. Compile and execute the above
program by executing the following commands in the command prompt.
\>javac H2jdbcInsertDemo.java
\>java H2jdbcInsertDemo
Read Record
In this example, we will write a program for reading records. Let us try to read all records
from the table Registration.
Following is an example program named H2jdbcRecordDemo.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
// Database credentials
static final String USER = "sa";
static final String PASS = "";
// Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
// STEP 5: Clean-up environment
rs.close();
} catch(SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch(Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// finally block used to close resources
try {
if(stmt!=null) stmt.close();
} catch(SQLException se2) {
} // nothing we can do
try {
if(conn!=null) conn.close();
} catch(SQLException se) {
se.printStackTrace();
} // end finally try
} // end try
System.out.println("Goodbye!");
}
}
Save the above program into H2jdbcReadDemo.java. Compile and execute the above
program by executing the following commands in the command prompt.
\>javac H2jdbcReadDemo.java
\>java H2jdbcReadDemo
Update Records
In this example, we will write a program to update records. Let us try to read all records
from the table Registration.
Following is an example program named H2jdbcUpdateDemo.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
// Database credentials
static final String USER = "sa";
static final String PASS = "";
while(rs.next()){
// Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
// Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
rs.close();
} catch(SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch(Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// finally block used to close resources
try {
if(stmt!=null) stmt.close();
} catch(SQLException se2) {
} // nothing we can do
try {
if(conn!=null) conn.close();
} catch(SQLException se) {
se.printStackTrace();
} // end finally try
} // end try
System.out.println("Goodbye!");
}
}
Save the above program into H2jdbcUpdateDemo.java. Compile and execute the above
program by executing the following commands in the command prompt.
\>javac H2jdbcUpdateDemo.java
\>java H2jdbcUpdateDemo
Delete Records
In this example, we will write a program to delete records. Let us try to read all records from
the table Registration.
Following is an example program named H2jdbcDeleteDemo.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
while(rs.next()){
// Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
// Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
rs.close();
} catch(SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch(Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// finally block used to close resources
try {
if(stmt!=null) stmt.close();
} catch(SQLException se2) {
} // nothing we can do
try {
if(conn!=null) conn.close();
} catch(SQLException se) {
se.printStackTrace();
} // end finally try
} // end try
System.out.println("Goodbye!");
}
}
Save the above program into H2jdbcDeleteDemo.java. Compile and execute the above
program by executing the following commands in the command prompt.
\>javac H2jdbcDeleteDemo.java
\>java H2jdbcDeleteDemo
Advertisements
PostgreSQL For Business -
Fujitsu
postgresql.fastware.com/fujitsu/postgresql
About us
©
Terms of use
Cookies Policy
FAQ's
Helping
Contact
Copyright 2019. All Rights Reserved.