JDBC Statement example to Delete a Record
Here is an example to show you how to delete a record from a table via JDBC statement.
To issue a delete statement, calls the Statement.executeUpdate() method as shown below :
Statement statement = connection.createStatement(); // execute delete SQL stetement statement.executeUpdate(deleteTableQuery);
Full example…
File: JDBCStatementDeleteExample.java
package com.simplecode.jdbc;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
public class JDBCStatementDeleteExample
{
private static final String dbDriver = "oracle.jdbc.driver.OracleDriver";
private static String serverName = "127.0.0.1";
private static String portNumber = "1521";
private static String sid = "XE";
private static final String dbUrl ="jdbc:oracle:thin:@"+serverName+":"+ portNumber+":"+sid;
private static final String dbUser = "system";
private static final String dbPassword = "admin";
public static void main(String[] argc)
{
try
{
deleteRecordFromEmployeeTable();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
private static void deleteRecordFromEmployeeTable() throws SQLException
{
Connection connection = null;
Statement statement = null;
String deleteTableQuery = "DELETE EMPLOYEE WHERE USER_ID = 1000";
try
{
connection = getDBConnection();
statement = connection.createStatement();
// execute delete SQL statement
statement.executeUpdate(deleteTableQuery);
System.out.println("Record is deleted from EMPLOYEE table!");
}
catch (SQLException e)
{
System.err.println(e.getMessage());
}
finally
{
if (statement != null)
{
statement.close();
}
if (connection != null)
{
connection.close();
}
}
}
private static Connection getDBConnection()
{
Connection dbConnection = null;
try
{
Class.forName(dbDriver);
}
catch (ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
try
{
dbConnection = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
return dbConnection;
}
catch (SQLException e)
{
System.err.println(e.getMessage());
}
return dbConnection;
}
}
![]() |
|
Read More
JDBC Statement Example to Update a Record
Here’s an example to show you how to update a record in a table via JDBC statement.
To issue an update statement, call the Statement.executeUpdate() method like this :
Statement statement = connection.createStatement(); // execute update SQL stetement statement.executeUpdate(updateTableQurey);
Full example…
File: JDBCStatementUpdateExample.java
package com.simplecode.jdbc;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
public class JDBCStatementUpdateExample
{
private static final String dbDriver = "oracle.jdbc.driver.OracleDriver";
private static String serverName = "127.0.0.1";
private static String portNumber = "1521";
private static String sid = "XE";
private static final String dbUrl = "jdbc:oracle:thin:@" + serverName + ":"
+ portNumber + ":" + sid;
private static final String dbUser = "system";
private static final String dbPassword = "admin";
public static void main(String[] argc)
{
try
{
updateRecordIntoEmployeeTable();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
private static void updateRecordIntoEmployeeTable() throws SQLException
{
Connection connection = null;
Statement statement = null;
String updateTableQurey = "UPDATE EMPLOYEE SET USERNAME = 'Azar' "
+ "WHERE USER_ID = 1000";
try
{
connection = getDBConnection();
statement = connection.createStatement();
// execute update SQL statement
statement.executeUpdate(updateTableQurey);
System.out.println("Record is updated to EMPLOYEE table!");
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
if (statement != null)
{
statement.close();
}
if (connection != null)
{
connection.close();
}
}
}
private static Connection getDBConnection()
{
Connection dbConnection = null;
try
{
Class.forName(dbDriver);
}
catch (ClassNotFoundException e)
{
System.out.println(e.getMessage());
}
try
{
dbConnection = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
return dbConnection;
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
return dbConnection;
}
}
![]() |
|
Read More
JDBC Statement example to Insert a record
Here’s an example to show you how to insert a record into table via JDBC statement.
To issue a insert statement, calls the Statement.executeUpdate() method like this :
Statement statement = connection.createStatement(); // execute the insert SQL stetement statement.executeUpdate(insertTableQuery);
Full example…
File: JDBCStatementInsertExample.java
package com.simplecode.jdbc;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
public class JDBCStatementInsertExample
{
private static final String dbDriver = "oracle.jdbc.driver.OracleDriver";
private static String serverName = "127.0.0.1";
private static String portNumber = "1521";
private static String sid = "XE";
private static final String dbUrl="jdbc:oracle:thin:@"+serverName+":"+portNumber+":"+sid;
private static final String dbUser = "system";
private static final String dbPassword = "admin";
public static void main(String[] argv) {
try
{
insertRecordIntoTable();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
private static void insertRecordIntoTable() throws SQLException
{
Connection connection = null;
Statement statement = null;
String insertTableQuery = "INSERT INTO EMPLOYEE"
+ "(USER_ID, USERNAME, CREATED_BY) VALUES"
+ "(1000,'nilafar','jamil')";
try
{
connection = getDBConnection();
statement = connection.createStatement();
// execute insert SQL statement
statement.executeUpdate(insertTableQuery);
System.out.println("Record is inserted into EMPLOYEE table!");
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
if (statement != null)
{
statement.close();
}
if (connection != null)
{
connection.close();
}
}
}
private static Connection getDBConnection()
{
Connection dbConnection = null;
try
{
Class.forName(dbDriver);
}
catch (ClassNotFoundException e)
{
System.out.println(e.getMessage());
}
try
{
dbConnection = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
return dbConnection;
}
catch (SQLException e)
{
System.out.println("Db "+e.getMessage());
}
return dbConnection;
}
}
![]() |
|
Read More
JDBC Statement example to Create a table
Here’s an example to show you how to create a table in database via JDBC statement.
To issue a create statement, call the Statement.execute() method like this :
Statement statement = connection.createStatement(); // execute create SQL stetement statement.execute(createTableQuery);
Full example…
File: JDBCStatementCreateExample.java
package com.simplecode.jdbc;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
public class JDBCStatementCreateExample
{
private static final String dbDriver = "oracle.jdbc.driver.OracleDriver";
private static String serverName = "127.0.0.1";
private static String portNumber = "1521";
private static String sid = "XE";
private static final String dbUrl ="jdbc:oracle:thin:@"+serverName+":"+ portNumber+":"+sid;
private static final String dbUser = "system";
private static final String dbPassword = "admin";
public static void main(String[] argc)
{
try
{
createTable();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
private static void createTable() throws SQLException
{
Connection connection = null;
Statement statement = null;
String createTableQuery = "CREATE TABLE EMPLOYEE(USER_ID NUMBER(5) "
+ "NOT NULL, USERNAME VARCHAR(20) NOT NULL, CREATED_BY VARCHAR(20)"
+ " NOT NULL, PRIMARY KEY (USER_ID) )";
try
{
connection = getDBConnection();
statement = connection.createStatement();
// execute the SQL statement
statement.execute(createTableQuery);
System.out.println("Table EMPLOYEE is created!");
}
catch (SQLException e)
{
System.err.println(e.getMessage());
}
finally
{
if (statement != null)
{
statement.close();
}
if (connection != null)
{
connection.close();
}
}
}
private static Connection getDBConnection()
{
Connection dbConnection = null;
try
{
Class.forName(dbDriver);
}
catch (ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
try
{
dbConnection = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
return dbConnection;
}
catch (SQLException e)
{
System.err.println(e.getMessage());
}
return dbConnection;
}
}
![]() |
|
Read More
Connect to PostgreSQL via JDBC driver
Here is an example to show you how to connect to PostgreSQL database with JDBC driver.
** UPDATE: Complete JDBC tutorial now available here.
1. Download PostgreSQL JDBC Driver
Get PostgreSQL JDBC driver here -
2. Java JDBC connection example
Code snippets to use JDBC to connect a PostgreSQL database
Class.forName("org.postgresql.Driver");
Connection connection = null;
connection = DriverManager.getConnection(
"jdbc:postgresql://hostname:port/dbname","username", "password");
connection.close();
See a complete example below:
File:PostgreSqlJDBC.java
package com.simplecode.jdbc;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class PostgreSqlJDBC {
private static final String DB_DRIVER = "org.postgresql.Driver";
private static final String DB_CONNECTION="jdbc:postgresql://127.0.0.1:5432/simplecodedb";
private static final String DB_USER = "username";
private static final String DB_PASSWORD = "password";
public static void main(String[] argc) {
System.out.println("***** PostgreSQL JDBC Connection Testing *****");
try
{
Class.forName(DB_DRIVER);
}
catch (ClassNotFoundException e)
{
System.err.println("Please add PostgreSQL JDBC Driver in your Classpath ");
System.err.println(e.getMessage());
return;
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try
{
connection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
}
catch (SQLException e)
{
System.err.println("Connection Failed, Check console");
System.err.println(e.getMessage());
return;
}
if (connection == null)
{
System.out.println("Connection Failed !");
}
else
{
System.out.println("Connection established!");
}
}
}
Read More
Connect to Oracle DB via JDBC driver
Here is an example to show you how to connect to Oracle database via JDBC driver.
1. Download Oracle JDBC Driver
Get Oracle JDBC driver here – ojdbcxxx.jar
2. Java JDBC connection example
Code snippets to use JDBC to connect a Oracle database.
** UPDATE: Complete JDBC tutorial now available here.
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = null;
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@hostname:port:dbname","username","password");
connection.close();
See a complete example below:
File: OracleJDBC.java
package com.simplecode.jdbc;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class OracleJDBC {
private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DB_CONNECTION="jdbc:oracle:thin:@localhost:1521:SIMPLECODEDB";
private static final String DB_USER = "username";
private static final String DB_PASSWORD = "password";
public static void main(String[] argc) {
System.out.println("**** Oracle JDBC Driver Connection Establishing ****");
try
{
Class.forName(DB_DRIVER);
}
catch (ClassNotFoundException e)
{
System.out.println("Exception : Add Oracle JDBC Driver in your classpath ");
System.err.println(e.getMessage());
return;
}
System.out.println("*Oracle JDBC Driver Registered!");
Connection connection = null;
try
{
connection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
}
catch (SQLException e) {
System.out.println("Connection Failed!);
System.err.println(e.getMessage());
return;
}
if (connection == null) {
System.out.println("Connection Failed!");
}
else
{
System.out.println("Connection established!");
}
}
}
Read More
