0% found this document useful (0 votes)
3 views9 pages

Ajp Pra 19

Uploaded by

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

Ajp Pra 19

Uploaded by

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

Gramin Technical and Management Campus

Department of computer engineering


Subject/code: -AJP/22517

Name:-Pranusha Shiddhodhan Birhade Batch:- A Roll no:- 67

DOP: DOS:-

Practical No 19 :- Write a program to demonstrate the use of PreparedStatement


ResultSet interface.

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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy