












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An introduction to java database connectivity (jdbc) api, explaining its concepts, important features, and steps to connect to a database using odbc driver. It covers creating a database, configuring the odbc driver, loading the driver, and executing sql queries. Additionally, it discusses exceptions that may occur and how to handle them.
Typology: Slides
1 / 20
This page cannot be seen from the preview
Don't miss anything!
An Introduction
Today’s Lecture
Connecting to Database
Exceptions
Class.forName DriverManager.getConnection Statement.executeQuery
Complete Code-
o package connection;
o import java.sql.; o import sun.jdbc.;
o public class DBTest {
o Connection conn; o Statement stm; o ResultSet records; o o public DBTest() { o }
o public void connect(){ o try { o Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); o conn=DriverManager.getConnection("jdbc:odbc:mydb","",""); o stm=conn.createStatement(); o } o catch (ClassNotFoundException e){System.out.println("Cannot Load Driver"+e);} o c atch (SQLException e){System.out.println("Error while connecting"+e);} o }
Output
o ---------------------------------------
o Employ ID: 1
o Employ Name : nauman
o City : Islmabad
o HomePhone :
o EmailAddress: nauman@pieas.edu.pk
o ---------------------------------------
o Employ ID: 2
o Employ Name : aslam
o City : Karachi
o HomePhone :
o EmailAddress: aslam@pieas.edu.pk
o ---------------------------------------
o Employ ID: 3
o Employ Name : anwar
o City : Lahore
o HomePhone :
o EmailAddress: anwar@pieas.edu.pk
DataBase Information
Statement Object
For executing a simple SQL statement
For executing a precompiled SQL statement passing in parameters
For executing a database stored procedure
[slide taken from internet]
Useful Mthods
ResultSet results =statement.executeQuery("SELECT a, b FROM table");
int rows = statement.executeUpdate("DELETE FROM EMPLOYEES" + "WHERE STATUS=0");
Prepared Statement
Example Code
Connection connection = DriverManager.getConnection(url, user, password);
PreparedStatement statement = connection.prepareStatement("UPDATE
employees "+ "SET salary =? " +"WHERE id = ?");
int[] newSalaries = getSalaries();
int[] employeeIDs = getIDs();
For (int i=0; i<employeeIDs.length; i++) {
statement.setInt(1, newSalaries[i]);
statement.setInt(2, employeeIDs[i]);
statement.executeUpdate();
}