JDBC
JDBC
• (ii). Connection
1.Statement createStatement(): Creates a Statement object for sending SQL statements to
the database.
2.PreparedStatement prepareStatement(String sql): Creates a PreparedStatement object for
sending parameterized SQL statements to the database.
3.CallableStatement prepareCall(String sql): Creates a CallableStatement object for
executing stored procedures.
4.void close(): Closes the connection to the database.
5.rollback(): Rolls back the current transaction.
• (iii) Statement
1.ResultSet executeQuery(String sql): Executes a SQL query and returns the result as
a ResultSet.
2.int executeUpdate(String sql): Executes a SQL statement that may change the database (e.g.,
INSERT, UPDATE, DELETE) and returns the number of affected rows.
3.Close(): Closes the statement.
• (iv) PreparedStatement
1.void setInt(int parameterIndex, int x): Sets the value of the designated parameter to the
given Java int value.
2.void setString(int parameterIndex, String x): Sets the value of the designated parameter to
the given Java String value..
3.int executeUpdate(): Executes the prepared statement that may change the database and
returns the number of affected rows.
4.executeQuery(): Executes a SELECT query.
5.execute(): Executes general SQL statements.
• (vi) CallableStatement
1.void registerOutParameter(int parameterIndex, int sqlType): Registers
the designated parameter as an output parameter.
2.ResultSet executeQuery(): Executes the stored procedure and returns the
result as a ResultSet.
3.int executeUpdate(): Executes the stored procedure that may change the
database and returns the number of affected rows.
• (vii) ResultSet
1.boolean next(): Moves the cursor to the next row in the ResultSet.
2.int getInt(String columnLabel): Retrieves the value of the designated column as an int.
3.String getString(String columnLabel): Retrieves the value of the designated column as
a String.
JDBC CYCLE:
OPERATIONS TO PERFORM:
Following operations can be performed:
• SELECT FROM DATABASE
• INSERT IN DATABASE
• UPDATE IN DATABASE
• DELETE FROM DATABASE
HOW TO CREATE DATABASE
To create database:
create database db;
=> Query OK , 1 row affected
To use that database:
Use db;
=> Database changed
To create table:
create table students(id int , age int, name varchar(50) , marks int);
=> Query OK , 0 rows affected
To insert items in table:
insert into students(id , age , name , marks) values (01 , 23 , “nayak” , 45);
=> Query OK , 1 row affected
To retrieve items from table:
select * from students;
=> 1 row in set
OUTPUT
JDBC CODE TO SELECT DATA FROM DATABSE
import java.sql.*;
public class App
{
private static final String url = "jdbc:mysql://localhoast:3306/db";
private static final String username = "root";
private static final String password = "priyanshi";
public static final String Query = "SELECT id, name FROM students";
public static void main(String ar[])
{
try{
Class.forName(className:"com.mysql.cj.jdbc.Driver");
} catch(ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try{
Connection con = DriverManager.getConnection(url , username , password);
Statement st = con.createStatement();
ResultSet rst= st.executeQuery(Query);
while(rs.next()){
System.out.println("id: " + rst.getInt("id"));
System.out.println(", name: " + rst.getString(“name"));
}
rst.close();
st.close();
con.close();
} catch (SQLException e){
System.out.println(e.getMessage());
}
}
}
OUTPUT