JDBC

JDBC – Load Driver

Class.forName(): package online;public class LoadDriver{            public static void main(String args[])            {                        String driver = “oracle.jdbc.driver.OracleDriver”;                        try                        {                                    Class.forName(driver);                                    System.out.println(“Driver loaded”);                        }                        catch(ClassNotFoundException e1)                        {                                    System.out.println(“Exception : Driver is not present”);                        }            }}

JDBC – Connection to DB

Connection to Database from Java Application: Input values are: Invoke method: Go through briefly if you want: package online;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DBConnection{            public static void main(String args[]) throws SQLException            {                        String driver = “oracle.jdbc.driver.OracleDriver”;                        Connection conn = null;                        try                        {                                    Class.forName(driver);                                    System.out.println(“Driver loaded”);                                                                       String url = “jdbc:oracle:thin:@localhost:1521:xe”;                                    String uname = “system”;                                    String pwd …

JDBC – Connection to DB Read More »

Connection and Statement Interfaces

Connection interface : public Statement createStatement() creates a statement object that can be used to execute SQL queries. public void setAutoCommit(boolean status) is used to set the commit status. By default, it is true. public void commit() saves the changes made since the previous commit/rollback is permanent. public void rollback() Drops all changes made since …

Connection and Statement Interfaces Read More »

JDBC – Create Table in Database

Create table: (static query) package online;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;public class CreateTable{            public static void main(String args[]) throws Exception            {                        Connection conn = null;                        String driver = “oracle.jdbc.driver.OracleDriver”;                        try                        {                                    Class.forName(driver);                                    System.out.println(“Driver loaded”);                                                                       String url = “jdbc:oracle:thin:@localhost:1521:xe”;                                    String uname = “system”;                                    String pwd = “admin”;                                                                       conn = DriverManager.getConnection(url, uname, pwd);                                    System.out.println(“Connection is ready”);                                                                            Statement stmt …

JDBC – Create Table in Database Read More »

JDBC – Delete All Records

Delete all records from table: (static query) executeUpdate() method: package online;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;public class DeleteRecords{            public static void main(String args[]) throws Exception            {                        Connection conn = null;                        String driver = “oracle.jdbc.driver.OracleDriver”;                        try                        {                                    Class.forName(driver);                                    System.out.println(“Driver loaded”);                                                                       String url = “jdbc:oracle:thin:@localhost:1521:xe”;                                    String uname = “system”;                                    String pwd = “admin”;                                                                       conn = DriverManager.getConnection(url, uname, pwd);                                    …

JDBC – Delete All Records Read More »

JDBC – Display All Records

Access all records from table: (static query) package online;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class AccessData{            public static void main(String args[]) throws Exception            {                        Connection conn = null;                        String driver = “oracle.jdbc.driver.OracleDriver”;                        try                        {                                    Class.forName(driver);                                    System.out.println(“Driver loaded”);                                    String url = “jdbc:oracle:thin:@localhost:1521:xe”;                                    String uname = “system”;                                    String pwd = “admin”;                                    conn = DriverManager.getConnection(url, uname, pwd);                                    System.out.println(“Connection …

JDBC – Display All Records Read More »

JDBC – Table info using ResultSetMetaData

import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.Statement;public class MetaData{            static Connection con;            public static void main(String args[]) throws Exception            {                        try                        {                                    Class.forName(“oracle.jdbc.driver.OracleDriver”);                                    System.out.println(“Driver is ready”);                                     con = DriverManager.getConnection( “jdbc:oracle:thin:@localhost:1521:xe”, “system”, “admin”);                                    System.out.println(“Connection is ready”);                                                                       Statement stmt = con.createStatement();                                    ResultSet rs = stmt.executeQuery(“select *from account”);                                    ResultSetMetaData rsmd = rs.getMetaData();                                                                       int count = rsmd.getColumnCount();                                    System.out.println(“Number of Columns …

JDBC – Table info using ResultSetMetaData Read More »

JDBC – PreparedStatement

PreparedStatement interface: Statement PreparedStatement It is used to execute static sql queries It is used to execute sql statement many times. Doesn’t accept input  parameters Access input parameters at runtime. Statement st; st=conn.createStatement(); PreparedStatement ps; String s=”Update Employees SET age=? WHERE id=?”; ps=conn.prepareStatement(s); Setter methods: PreparedStatement providing setter methods by which we can set values …

JDBC – PreparedStatement Read More »

JDBC – Read and Insert Record

package online;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;import java.sql.Statement;import java.util.Scanner;public class InsertRecord{            public static void main(String args[]) throws Exception            {                        Connection conn = null;                        String driver = “oracle.jdbc.driver.OracleDriver”;                        try                        {                                    Class.forName(driver);                                    System.out.println(“Driver loaded”);                                    String url = “jdbc:oracle:thin:@localhost:1521:xe”;                                    String uname = “system”;                                    String pwd = “admin”;                                                                       conn = DriverManager.getConnection(url, uname, pwd);                                    System.out.println(“Connection is ready”);                                                                            String query = “insert …

JDBC – Read and Insert Record Read More »

Scroll to Top