0% found this document useful (0 votes)
3 views

JAVA_PRACTICAL19

Uploaded by

Priyanka Thadke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JAVA_PRACTICAL19

Uploaded by

Priyanka Thadke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Name:- Priyanka Khandu Thadke

Class:-A(CO5I) Roll no:- 60


Practical no:- 19 Write a Program to demonstrate use of prepared statement ,Resultset
Statement Interface.

Program code:-

Q.1)write the program to update rows of student table from msbte database
using mysql database server
Code:
import java.sql.*;
public class Pr19pc1
{
public static void main(String args[])
{
try
{
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/VRD","root","root");

PreparedStatement stmt=con.prepareStatement("update student set name=(?) where


rollno=101");

stmt.setString(1,"student");

int i=stmt.executeUpdate();
System.out.println(i+" records updated");
con.close();
}
catch(Exception e)
{
System.out.println(e);}
}
}

Output:
2. write the output of following jdbc code

Code:
import java.sql.*;
public class PreparedStmtEx
{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/VRD","root","root");
//keep “” empty if not given during installation

PreparedStatement stmt=con.prepareStatement("insert into student values(?,?,?)");


stmt.setInt(1,101);
//1 specifies the first parameter(1st ? symbol) in the query
stmt.setString(2,"Abhishek");
//2 specifies the second parameter(2nd ? symbol) in the query
stmt.setString(3,"Yadav");
//3 specifies the third parameter(3rd ? symbol) in the query
int i=stmt.executeUpdate();
System.out.println(i+" records inserted");
con.close();
}
catch(Exception e)
{
System.out.println(e);}
}
}
Output:

Exercise:-

Q.1)Develop JDBC program to retrieve data using ResultSet.


Code:

package connect;
import java.sql.*;
public class pr19_ex1
{
public static void main(String argd[])

{ try

{
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/dbtest ","root",
"bidwai@14");
Statement s = con.createStatement();
s.execute("select * from stud");
ResultSet rs = s.getResultSet();
String text = " ";
System.out.println("Roll Number \t Name \\t\\t

Percentage");

while (rs.next()) { text = text + rs.getInt(1) "\t\

t+ rs.getString(2) + "\t\t"+ rs.getString(4) +"\

n"; } System.out.print(text);

s.close(); con.close(); }

catch (SQLException s) {

System.out.println("sql error");
}
}}

Output:

2.Develop a program to update a record in database table.


Code:

package connect;
import java.sql.*;

public class Pr19_ex2


{
public static void main(String args[])
{ try

{
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/dbtest ","root", "bidwai@14");
String a = "select * from stud";
String b = "UPDATE stud SET name='Suhani' where rollno=102";

Statement s = con.createStatement();
s.execute(a);
ResultSet r=s.executeQuery(a); while(r.next())

System.out.println(r.getInt(1)+"\t"+r.getString(2)+"\t"+r.getInt(3)+"\t"
+r.getInt(4));
}
int r1= s.executeUpdate(b);
System.out.println(r1+" row Updated");

ResultSet rs=s.executeQuery("select * from stud");

while(rs.next())

System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getInt(3)+ "\t"+rs.getInt(4));
}
s.close(); con.close();

}
catch (SQLException s)
{
System.out.println("sql error");
}
}
}

You might also like