Unit 5 Notes On Ajp 22517 Part 3
Unit 5 Notes On Ajp 22517 Part 3
The question marks '?' as shown above represents placeholder for the input parameter.
The input parameters are set using setInt(), setString() like methods.
Methods:
public boolean execute() throws SQLException
o Executes the SQL statement in this PreparedStatement object, which may be any
kind of SQL statement. Some prepared statements return multiple results. If the
first result is ResultSet object then this method returns true;otherwise returns
false.
public int executeUpdate() throws SQLException
o Executes the SQL statement in this PreparedStatement object, which must be an
SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or
DELETE; or an SQL statement that returns nothing, such as a DDL statement. It
returns the number of rows affected after execution.
public ResultSet executeQuery() throws SQLException
o Executes the SQL query in this PreparedStatement object and returns the
ResultSet object generated by the query.
public void setDouble(int parameterIndex, double x) throws SQLException
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
o Sets the designated parameter to the given Java double value. The first parameter
is 1, the second is 2, ... The driver converts this to an SQL DOUBLE value when it
sends it to the database.
public void setInt(int parameterIndex, int x) throws SQLException
o Sets the designated parameter to the given Java int value. The first parameter is 1,
the second is 2, ... The driver converts this to an SQL INT value when it sends it to
the database.
public void setString(int parameterIndex, String x) throws SQLException
o Sets the designated parameter to the given Java String value. The first parameter
is 1, the second is 2, ... The driver converts this to an SQL VARCHAR value when it
sends it to the database.
public void setType(int parameterIndex, Type x) throws SQLException
o Sets the designated parameter to the given Java Type value. The first parameter is
1, the second is 2, ... The driver converts this to an SQL TYPE value when it sends it
to the database.
================================================================
Program to execute DML statements (INSERT INTO, DELETE FROM, UPDATE…SET) using
PreparedStatement using MySQL and Java
================================================================
Aim : To insert single record in student table of college database in MySQL.
import java.sql.*;
import java.io.*;
@SuppressWarnings("deprecation")
class Mydatabase {
public static void main(String[] args) {
try {
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
// Create a precompiled parameterised statement
String query="insert into student values(?,?,?,?)";
PreparedStatement pstmt = con.prepareStatement(query);
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
Aim : To insert multiple records in student table of college database in MySQL.
import java.sql.*;
import java.io.*;
@SuppressWarnings("deprecation")
class Mydatabase {
public static void main(String[] args) {
try {
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
Connection con = DriverManager.getConnection(url, username, password);
Aim : To delete a record of student whose roll number is entered by user from student
table of college database in MySQL.
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
import java.sql.*;
import java.io.*;
@SuppressWarnings("deprecation")
class Mydatabase {
public static void main(String[] args) {
try {
Aim : To change the branch of student from co to computer whose rollno is entered by
user.
import java.sql.*;
import java.io.*;
@SuppressWarnings("deprecation")
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
class Mydatabase {
public static void main(String[] args) {
try {
================================================================
Program to execute DQL statements (SELECT * FROM) using PreparedStatement using
MySQL and Java
================================================================
Aim : To display all records of student table of college database in MySQL.
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
import java.sql.*;
import java.io.*;
@SuppressWarnings("deprecation")
class Mydatabase {
public static void main(String[] args) {
try {
while(rs.next())
{
System.out.print(rs.getInt(1) + " ");
System.out.print(rs.getString(2) + " ");
System.out.print(rs.getString(3) + " ");
System.out.print(rs.getString(4) + " ");
System.out.println();
}
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
// Close the connection
pstmt.close();
rs.close();
con.close();
} catch (Exception e) {
System.out.println("Error:"+e);
}
}
}
Aim : To display first and last records of student table of college database in MySQL.
import java.sql.*;
import java.io.*;
@SuppressWarnings("deprecation")
class Mydatabase {
public static void main(String[] args) {
try {
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
PreparedStatement pstmt =
con.prepareStatement(query,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDA
TABLE);
Aim : To insert record in student table using ResultSet object and display all information
from student table after insertion.
import java.sql.*;
import java.io.*;
@SuppressWarnings("deprecation")
class Mydatabase {
public static void main(String[] args) {
try {
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String option="Y";
do
{
Lecture Notes On Advanced Java Programming (AJP-22517) Under MSBTE I-SCHEME Prepared By Prof. S.M. Inwate