JDBC Example

Download Report

Transcript JDBC Example

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;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver"); } Loading the database driver
conn=DriverManager.getConnection("jdbc:mysql://localhost/game","root","password");
stmt=conn.createStatement();
Creating statement Object
Making the connection
}
Query="select * from table1";
Executeing SQL Statements
rs=stmt.executeQuery(Query);
while(rs.next())
{
String s = rs.getString(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.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.
Making the connection
3) Making connection
conn=DriverManager.getConnection("jdbc:mysql://localhost/game","root","p
assword");
- 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.
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
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().



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.
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();
}
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
Thank you