program by using JDBC to execute an update query using PreparedStatement and display the results
Aim: Write a program by using JDBC to execute an update query using PreparedStatement and display the results.
Theory:
PreparedStatement interface:
The
PreparedStatement interface is a subinterface of Statement. It is used to
execute parameterized query.
Let's
see the example of parameterized query:
String sql="insert into emp values(?,?,?)";
As you
can see, we are passing parameter (?) for the values. Its value will be set by
calling the setter methods of PreparedStatement.
Improves
performance: The performance of the application will be faster if you use
PreparedStatement interface because query is compiled only once.
To get the instance of PreparedStatement:
The
prepareStatement() method of Connection interface is used to return the object
of PreparedStatement. Syntax:
1.
public PreparedStatement prepareStatement(String query)throws SQLException{}
Methods of PreparedStatement interface:
The
important methods of PreparedStatement interface are given below:
Method
|
Description
|
public void setInt(int paramIndex, int value)
|
sets the integer value to the given parameter index.
|
public void setString(int paramIndex, String value)
|
sets the String value to the given parameter index.
|
public void setFloat(int paramIndex, float value)
|
sets the float value to the given parameter index.
|
public void setDouble(int paramIndex, double value)
|
sets the double value to the given parameter index.
|
public int executeUpdate()
|
executes the query. It is used for create, drop, insert, update,
delete etc.
|
public ResultSet executeQuery()
|
executes the select query. It returns an instance of ResultSet.
|
Program:
import java.sql.*;
import java.util.*;
class Update1
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =DriverManager.getConnection("jdbc:odbc:xe","system","swapna");
PreparedStatement ps=c.prepareStatement("update student36 set name='diya shirley' where num=11");
System.out.println("\n\nUpdated\n\n");
ps.executeUpdate();
Statement st= c.createStatement();
ResultSet r1=st.executeQuery("select *from student36");
while(r1.next())
{
int x = r1.getInt("NUM");
String y= r1.getString("NAME");
System.out.println(x+"\t "+y);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.util.*;
class Update1
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =DriverManager.getConnection("jdbc:odbc:xe","system","swapna");
PreparedStatement ps=c.prepareStatement("update student36 set name='diya shirley' where num=11");
System.out.println("\n\nUpdated\n\n");
ps.executeUpdate();
Statement st= c.createStatement();
ResultSet r1=st.executeQuery("select *from student36");
while(r1.next())
{
int x = r1.getInt("NUM");
String y= r1.getString("NAME");
System.out.println(x+"\t "+y);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Output:
Labels: Advanced Java Lab
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home