JDBC – provides an interface to Relational Data Sources
JDBC library provides the means for executing SQL statements to access and operate on a relational database
JDBC library is implemented in the java.sql package
Set of classes and interfaces that provide a uniform API for access to broad range of databases.
JDBC library provides the means for executing SQL statements to access and operate on a relational database
JDBC library is implemented in the java.sql package
Set of classes and interfaces that provide a uniform API for access to broad range of databases.
JDBC’s
design is very similar to the design of ODBC
•Driver
Manager: Loads database drivers, and manages the connection
between the application and the driver
•Driver: Translates API calls into operations for a specific data
source
•Connection: A session between an application and a database
•Statement: An SQL Statement to
perform a query or update operation
•Metadata: Information about
returned data, the database and the driver
•ResultSet
: Logical set of
columns and rows returned by executing an SQL statement
The following steps are executed when running a JDBC application
1. Import the necessary classes
2. Load the JDBC driver
3. Identify the database source
4. Allocate a “connection” object (create)
5. Allocate a “Statement” object (create)
6. Execute a query using the “Statement” object
7. Retrieve data from the returned “ResultSet” object
8. Close the “ResultSet” object
9. Close the “Statement” object
10. Close the “Connection” object
Java Database connectivity (jdbc) program to show list of records from sql server tables.
import java.sql.*; public class dbselect { public static void main(String [] args) { Connection con = null; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:jdtest"); Statement sta = con.createStatement(); ResultSet res = sta.executeQuery("SELECT * FROM emp"); System.out.println("List of Records "); while (res.next()) { System.out.println(" "+res.getString("ecode") + ", "+res.getString("ename")); } res.close(); sta.close(); con.close(); } catch (Exception e) { System.err.println("Exception: "+e.getMessage()); } } }
Java - JDBC Program to insert records into a table.
import java.sql.*; public class dbaccess2 { public static void main(String [] args) { Connection con = null; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:jdtest"); Statement sta = con.createStatement(); int count = 0; int c = sta.executeUpdate("INSERT INTO sal" + " (basic, hra, net)" + " VALUES (4444, 44442, 3333)"); count = count + c; System.out.println("Number of rows inserted: "+count); sta.close(); con.close(); } catch (Exception e) { System.err.println("Exception: "+e.getMessage()); } } }
Delete records from table using jdbc example
import java.sql.*; public class delaccess { public static void main(String [] args) { Connection con = null; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:test"); Statement sta = con.createStatement(); int count = 0; int c = sta.executeUpdate("DELETE from emp WHERE ename='rahul'"); count = count + c; System.out.println("Number of rows DELETED: "+count); sta.close(); con.close(); } catch (Exception e) { System.err.println("Exception: "+e.getMessage()); } } }
No comments:
Post a Comment