0% found this document useful (0 votes)
24 views1 page

Java Ass-6

mmd;lqm;

Uploaded by

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

Java Ass-6

mmd;lqm;

Uploaded by

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

public class StudentDb {

public static void main(String[] args) {

Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/nakshatra","root","nakshat
ra12");
String url="jdbc:mysql://localhost:3306/nakshatra";
String username="root";
String password="nakshatra12";

//DELETE AN ENTRY
String sql = "delete from student where student_id=?";
try{
Connection connection = DriverManager.getConnection(url, username, password);
java.sql.PreparedStatement stmt = connection.prepareStatement(sql)){
stmt.setInt(1, 1002);
stmt.executeUpdate();
System.out.println("delete successful");
}

//INSERT AN ENTRY
try{
Connection connection = DriverManager.getConnection(url, username, password);
java.sql.PreparedStatement psmt = connection.prepareStatement("insert into student
values(?,?,?)");
psmt.setString(1,"1003");
psmt.setString(2,"sam");
psmt.setString(3,"female");
psmt.executeUpdate();
System.out.println("insert successful");

//update try{

Connection connection = DriverManager.getConnection(url, username, password);


java.sql.PreparedStatement psmt = connection.prepareStatement("updatestudent set
student_name='ram'"+" where student_id in (1,1003)");
//String query="update student set student_name='vijay'"+" where student_id in
(1,1003)";
psmt.executeUpdate();
System.out.println("update successful");

}
catch(Exception e){
System.out.println("Couldn't connect to database");
e.printStackTrace();
}
}

You might also like