0% found this document useful (0 votes)
12 views4 pages

AJP Practical 19

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)
12 views4 pages

AJP Practical 19

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/ 4

Practical 19 :Write a program to demonstrate the use of PreparedSattement and ResultSet– 2200100176 Vedika Mohite

Program code 1: Write a program to update a row of student table from MSBTE database.


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCProgram9 {

public static void main(String[] args) throws SQLException {


Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/msbte","root",
"root");
Statement stmt=con.createStatement();
String query="update stud set name='vedika',marks='90' where
rollno=7";
int cn=stmt.executeUpdate(query);
if(cn!=0)
{
System.out.println("No of rows updated="+cn);
}

Output :-

Vedika Mohite
Practical 19 :Write a program to demonstrate the use of PreparedSattement and ResultSet– 2200100176 Vedika Mohite

Program code 2: Write the output of following JDBC code.


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCProgram10 {

public static void main(String[] args){


Connection con;
try {
con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata","root","ro
ot");
PreparedStatement stmt=con.prepareStatement("insert into
student values(?,?,?)");
stmt.setInt(1, 101);
stmt.setString(2, "Abhishek");
stmt.setString(3, "Yadav");
int i=stmt.executeUpdate();
System.out.println(i+" record inserted");
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e);
}
}
}

Output :-

Vedika Mohite
Practical 19 :Write a program to demonstrate the use of PreparedSattement and ResultSet– 2200100176 Vedika Mohite

Exercise Q.1: Develop a JDBC Program to retrieve data using ResultSet.

import java.sql.*;
public class JDBCProgram2 {

public static void main(String[] args) throws SQLException {


Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata",
"root", "root");
Statement smt=con.createStatement();
ResultSet rs=smt.executeQuery("Select * from emp");
while(rs.next()) {
System.out.println("Name="+rs.getString(2)+"
Salary="+rs.getString("salary"));
}
}

Output :-

Vedika Mohite
Practical 19 :Write a program to demonstrate the use of PreparedSattement and ResultSet– 2200100176 Vedika Mohite

Exercise Q.2: Develop a Program to update a record in database table.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCProgram9 {

public static void main(String[] args) throws SQLException {


Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata","root"
,"root");
Statement stmt=con.createStatement();
String query="update stud set name='vedika',marks='90' where
rollno=7";
int cn=stmt.executeUpdate(query);
if(cn!=0)
{
System.out.println("No of rows updated="+cn);
}

Output :-

Vedika Mohite

You might also like