JDBCConnection - Statement Interface
JDBCConnection - Statement Interface
Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class InsertDataDemo {
public static void main(String[] args) {
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager
.getConnection("jdbc:mysql://localhost:3306/JDBCDemo", "root", "password");
Statement stmt = conn.createStatement();
stmt.execute("INSERT INTO EMPLOYEE (ID,FIRST_NAME,LAST_NAME,STAT_CD) "
+ "VALUES (1,'Lokesh','Gupta',5)");
}
catch (Exception e) {
e.printStackTrace();
}finally {
try {
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
} } }
Connection interface
• A Connection is the session between java application and database. The
Connection interface is a factory of Statement, PreparedStatement,
CallableStatement, and DatabaseMetaData
import java.sql.*;
class FetchRecord{
public static void main(String args[])throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","
oracle");
Statement stmt=con.createStatement();
stmt.executeUpdate("insert into emp765 values(33,'Irfan',50000)");
int result=stmt.executeUpdate("update emp765 set name='Vimal',salary=10000 where id=33");
int result=stmt.executeUpdate("delete from emp765 where id=33");
System.out.println(result+" records affected");
con.close();
}}
Prepared Statement
• The PreparedStatement interface is a subinterface of Statement. It is used to execute
parameterized query.
• example of parameterized query:
• String sql="insert into emp values(?,?,?)";
• The performance of the application will be faster if you use PreparedStatement
interface because query is compiled only once.
Method Description
public void setInt(int paramIndex, int sets the integer value to the given
value) parameter index.
public void setString(int paramIndex, sets the String value to the given
String value) parameter index.
public void setFloat(int paramIndex, float sets the float value to the given
value) parameter index.
public void setDouble(int paramIndex, sets the double value to the given
double value) parameter index.
public int executeUpdate() executes the query. It is used for create,
drop, insert, update, delete etc.
int i=stmt.executeUpdate();
System.out.println(i+" records deleted");
Result Set
• The object of ResultSet maintains a cursor pointing to a row of a table. Initially,
cursor points to before the first row.
1) public boolean next(): is used to move the cursor to the one row next from
the current position.
2) public boolean previous(): is used to move the cursor to the one row previous
from the current position.
3) public boolean first(): is used to move the cursor to the first row in result
set object.
4) public boolean last(): is used to move the cursor to the last row in result
set object.
5) public boolean absolute(int row): is used to move the cursor to the specified row
number in the ResultSet object.
6) public boolean relative(int row): is used to move the cursor to the relative row
number in the ResultSet object, it may be positive
or negative.
7) public int getInt(int columnIndex): is used to return the data of specified column index
of the current row as int.
8) public int getInt(String columnName): is used to return the data of specified column name
of the current row as int.
9) public String getString(int columnIndex): is used to return the data of specified column index
of the current row as String.
10) public String getString(String is used to return the data of specified column name
columnName): of the current row as String.
Example
import java.sql.*;
public class jdbcResultSet {
public static void main(String[] args)
{
try {
Class.forName(“com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e)
{
System.out.println("Class not found "+ e);
}
try
{
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/sonoo"
,"root","root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM employee");