JAVA_PRACTICAL19
JAVA_PRACTICAL19
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");
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
Exercise:-
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");
n"; } System.out.print(text);
s.close(); con.close(); }
catch (SQLException s) {
System.out.println("sql error");
}
}}
Output:
package connect;
import java.sql.*;
{
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");
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");
}
}
}