Presentation is loading. Please wait.

Presentation is loading. Please wait.

JDBC Example.

Similar presentations


Presentation on theme: "JDBC Example."— Presentation transcript:

1 JDBC Example

2 Java Database Connectivity
Steps Involved: loading the driver making the connection. Creating a jdbc statement object Execute SQL statement Getting the result set

3 Example } import statements } Loading the database driver
import java.sql.*; import java.io.*; class JDBC { public static void main(String[] args) try Connection conn; Statement stmt; String Query; ResultSet rs; Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost/game","root","password"); stmt=conn.createStatement(); } import statements } Loading the database driver Creating statement Object Making the connection

4 }Handling exceptions }Getting result set and displaying the details
}Executeing SQL Statements Query="select * from table1"; rs=stmt.executeQuery(Query); while(rs.next()) { String s = rs.getString(1); System.out.println(s); } rs.close(); conn.close(); catch(SQLException sqle) { System.out.println(sqle); catch(Exception e) System.out.println(e); }Getting result set and displaying the details }Handling exceptions

5 Loading the Driver a) Import statements import java.sql.*; b) Loading the database driver Class.forName("com.mysql.jdbc.Driver"); - We load the driver class by calling Class.forName() with the Driver class name as an argument - If the class name is jdbc.DriverXYZ , you would load the driver with the following line of code: Class.forName("jdbc.DriverXYZ"); - Once loaded, the Driver class creates an instance of itself.

6 Making the connection 3) Making connection conn=DriverManager.getConnection("jdbc:mysql://localhost/game","root","password"); - JDBC DriverManager defines objects which can connect Java applications to JDBCdriver - Its getConnection() method is used to establish a connection to a database. - It uses a username, password, and a jdbc url to establish a connection to the database and returns a connection object - JDBC URL Syntax:: jdbc: <subprotocol>: <subname> Example: For example, we're using the jdbc mysql subprotocol, so the DriverManager knows to use the com.mysql.jdbc.Driver.

7 Create a jdbc statement object
4) A statement object is used to send and execute SQL statements to a database Statement stmt; stmt=conn.createStatement(); - Connection interface defines methods for interacting with the database via the established connection

8 Executing SQL satement
5) Query="select * from table1"; rs=stmt.executeQuery(Query); - Statement interface defines methods that are used to interact with database via the execution of SQL statements - The Statement class has three methods for executing statements: executeQuery() executeUpdate() execute().

9 boolean execute() Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement. If you dont know which method to be used for executing SQL statements, this method can be used. This will return a boolean. TRUE indicates the result is a ResultSet and FALSE indicates it has the int value which denotes number of rows affected by the query. Rarly used, ResultSet executeQuery() Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query. Generally SELECT statement is used. int executeUpdate() Executes the SQL statement in this PreparedStatement object, which must be an SQL INSERT, UPDATE or DELETE statement; or an SQL statement that returns nothing, such as a DDL statement. The output will be in the form of int. This int value denotes the number of rows affected by the query.

10 Excute update() Connetion conn=DriverManager.getConnection("jdbc:mysql://localhost/game","root","password"); Preparedstatement = statement=conn.preparedstatement( “update employees”+”Set salary=?” +”Where id=?”); Int [] newsalaries=getsalaries(); Int [] employeeIds=getIds(); for (i=0 ; i<employeeId.length;i++) { statement . setInt(1,newsalaries[i]); sataement.setint(2, employeeids[i]); Statement.excuteupdate(); }

11 Result set 6) while(rs.next()) { String s = rs.getString(1);
System.out.println(s); } ResultSet provides access to a table of data generated by executing a Statement A ResultSet maintains a cursor pointing to its current row of data. The next() method is used to successively step through the rows of the tabular results 7) rs.close(); conn.close(); 8)Exceptions must be handled

12 Thank you


Download ppt "JDBC Example."

Similar presentations


Ads by Google