JDBC Example Java Database Connectivity Steps Involved 1

JDBC Example

Java Database Connectivity Steps Involved: (1) (2) (3) (4) (5) loading the driver making the connection. Creating a jdbc statement object Execute SQL statement Getting the result set

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

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

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

Making the connection 3) Making connection conn=Driver. Manager. get. Connection("jdbc: mysql: //localhost/game", "root", "pa ssword"); - JDBC Driver. Manager defines objects which can connect Java applications to JDBCdriver - Its get. Connection() 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 Driver. Manager knows to use the com. mysql. jdbc. Driver.

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

Executing SQL satement 5) Query="select * from table 1"; rs=stmt. execute. Query(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: execute. Query() execute. Update() execute().

n n n boolean execute() Executes the SQL statement in this Prepared. Statement 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 Result. Set and FALSE indicates it has the int value which denotes number of rows affected by the query. Rarly used, Result. Set execute. Query() Executes the SQL query in this Prepared. Statement object and returns the Result. Set object generated by the query. Generally SELECT statement is used. int execute. Update() Executes the SQL statement in this Prepared. Statement 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.

Excute update() Connetion conn=Driver. Manager. get. Connection("jdbc: mysql: //localhost/game", "root", "password"); Preparedstatement = statement=conn. preparedstatement( “update employees”+”Set salary=? ” +”Where id=? ”); Int [] newsalaries=getsalaries(); Int [] employee. Ids=get. Ids(); for (i=0 ; i<employee. Id. length; i++) { statement. set. Int(1, newsalaries[i]); sataement. setint(2, employeeids[i]); Statement. excuteupdate(); }

Result set 6) while(rs. next()) { String s = rs. get. String(1); System. out. println(s); } Result. Set provides access to a table of data generated by executing a Statement A Result. Set 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

Thank you
- Slides: 12