Series: JDBC

Java connecting to database programs explained in simple terms and screenshots

Select Records JDBC Example

Select Records JDBC Example: To retrieve records from Employee table in Oracle Database. By the time this program is executed, see some records exist in Employee table. Example on Select Records JDBC Example import java.sql.*; public class RetrieveRecords { public static void main(String args[]) throws Exception { // standard 3 JDBC statements Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); Connection con …

Select Records JDBC Example Read More »

Delete Record JDBC Example

Delete Record JDBC Example. The Employee ID is taken from keyboard input. Example on Delete Record JDBC Example import java.sql.*; import java.io.*; public class DeleteRecord { public static void main(String args[]) throws Exception { // to take input from the user BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Please Enter the ID no:”); String str=br.readLine(); int …

Delete Record JDBC Example Read More »

JDBC ResultSet Types and Concurrency

ResultSet Types Concurrency JDBC Overloaded createStatement() Method (knowledge required for ResultSet Types Concurrency JDBC). Before going into programming it is required to know more about createStatement() method of Connection interface. This method can have parameters. You know the following Statement object creation earlier. Statement stmt = con.createStatement(); The createStatement() method of Connection interface returns an …

JDBC ResultSet Types and Concurrency Read More »

Drop Table JDBC

Drop Table JDBC Following Drop Table JDBC code deletes the table Employee. All the records also deleted with the table. drop table SQL command is used. Example on Drop Table JDBC import java.sql.*; public class DeleteTable { public static void main(String args[]) throws Exception { // 3 standard JDBC statements as usual Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); Connection con=DriverManager.getConnection(“jdbc:odbc:snrao”,”scott”,”tiger”); …

Drop Table JDBC Read More »

Scrollable ResultSet

You retrieved records from database earlier in Select Records Example by iterating the records with the ResultSet object as follows. In the same example, you also have seen the ResultSet cursor movement in while loop. while(res.next()) { System.out.println(res.getInt(1) + “\t” + res.getString(2) + “\t” + res.getDouble(3)); } The above loop prints the records from top …

Scrollable ResultSet Read More »

Insert File JDBC

To store a file in Oracle table though JDBC (Insert File JDBC) Before going into execution, create table by name office with columns FILEID (number) and FILENAME (CLOB). Example on Insert File JDBC import java.sql.*; public class FileInsert { public static void main(String args[]) throws Exception { Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); Connection con = DriverManager.getConnection(“jdbc:odbc:snrao”, “scott”, “tiger”); PreparedStatement …

Insert File JDBC Read More »

Retrieve File JDBC

You have seen earlier how to store a file in database table office. Let us retrieve the file (Retrieve File JDBC), "Expenditure.txt" from table and write into another file "Liabilities.txt". Example in Retrieve File JDBC import java.io.*; import java.sql.*; public class RetrieveFile { public static void main(String[] args) throws Exception { Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); Connection con = …

Retrieve File JDBC Read More »

Difference Statement PreparedStatement CallableStatement

Statement vs PreparedStatement vs CallableStatement We know earlier, Statement interface works as a courier service between a Java (JDBC) program and database. There comes 3 types Statement interfaces in JDBC API doing the same job but used for different purposes. Let us see how they differ in Statement vs PreparedStatement vs CallableStatement and use them …

Difference Statement PreparedStatement CallableStatement Read More »

Dynamic SQL with PreparedStatement JDBC

In the following program, an Employee record is inserted using PreparedStatement(executing dynamic SQL statements). PreparedStatement along with CallableStatement is fully discussed in Difference between Statement, PreparedStatement, CallableStatement. Example on PreparedStatement JDBC import java.sql.*; import java.io.*; public class PreparedDemo { public static void main(String args[]) throws Exception { Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); Connection con = DriverManager.getConnection(“jdbc:odbc:snrao”, “scott”, “tiger”); // …

Dynamic SQL with PreparedStatement JDBC Read More »