Java JDBC Apache Derby Commands
Java JDBC Apache Derby Commands
Java DB is a distribution of the Apache Derby open-source relational database. It is written in pure Java,
and supports SQL through the JDBC and Java EE APIs. It can be run either embedded or as client/server.
Java Examples - Connect to a Database
Problem Description:
How to connect to a database using JDBC? Assume that database name is testDb and it has table
named employee which has 2 records.
Solution:
import java.sql.*;
The above code sample will produce the following result.The result may vary. You will get
ClassNotfound exception if your JDBC driver is not installed properly.
Solution:
Following example uses create, alter & drop SQL commands to create, edit or delete table
import java.sql.*;
The above code sample will produce the following result.The result may vary.
Solution:
Following example uses getString,getInt & executeQuery methods to fetch & display the
contents of the table.
import java.sql.*;
Result:
The above code sample will produce the following result.The result may vary.
id name job
1 alok trainee
2 ravi trainee
Solution:
Following method uses update, delete & insert SQL commands to edit or delete row contents.
import java.sql.*;
Result:
The above code sample will produce the following result.The result may vary.
id name job
2 ravi trainee
1 ronak manager
Solution:
Following method uses where & like sql Commands to search through the database.
import java.sql.*;
Result:
The above code sample will produce the following result.The result may vary.
Solution:
import java.sql.*;
Result:
The above code sample will produce the following result.The result may vary.
Following example uses inner join sql command to combine data from two tables. To display the
contents of the table getString() method of resultset is used.
import java.sql.*;
Result:
The above code sample will produce the following result.The result may vary.
import java.sql.*;
Result:
The above code sample will produce the following result.The result may vary.
Following method uses update, delete & insert SQL commands to edit or delete row contents.
import java.sql.*;
Result:
The above code sample will produce the following result.The result may vary.
id name job
2 ravi trainee
1 ronak manager
Java Examples - Use of prepared statement
Problem Description:
Solution:
import java.sql.*;
Result:
The above code sample will produce the following result.The result may vary.
Id Name Job
23 Roshan CEO
Java Examples - Use of savepoint & rollback
Problem Description:
Solution:
import java.sql.*;
Result:
The above code sample will produce the following result.The result may vary.
Solution:
Following example uses addBatch & executeBatch commands to execute multiple SQL
commands simultaneously.
import java.sql.*;
Result:
The above code sample will produce the following result.The result may vary.
rows before batch execution= 6
Batch executed
rows after batch execution= = 9
How to use different row methods to get no of rows in a table, update table etc?
Solution:
import java.sql.*;
Result:
The above code sample will produce the following result.For this code to compile your database
has to be updatable. The result may vary.
No of rows in table=5
Row added
first row deleted
How to use different methods of column to Count no of columns, get column name, get column
type etc ?
Solution:
import java.sql.*;
Result:
The above code sample will produce the following result.The result may vary.