Java 2 Assign
Java 2 Assign
Program Code :
package jdbc;
import java.sql.*;
import java.util.*;
public class jdbcfetch {
public static void main(String args[]) {
try {
//fetch query
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/carproject", "root",
"");
//delete query
String sql = "DELETE FROM cars WHERE car_id =20";
PreparedStatement statement = con.prepareStatement(sql);
statement.executeUpdate();
System.out.println("Record deleted.");
//insert query
String sql1 = "insert into cars
values(4,'BMW','PETROL',8,800000,'buggati.jgp','Y')";
PreparedStatement statement1 = con.prepareStatement(sql1);
statement1.executeUpdate();
System.out.println("select*from cars");
//update query
String sql2 = "UPDATE cars SET car_name='Buggati' WHERE car_id =
4";
PreparedStatement statement2 = con.prepareStatement(sql2);
statement2.execute();
System.out.println("Record updated.");
while (rs.next())
System.out.println(rs.getInt(1) + " " + rs.getString(2) + "
" + rs.getString(3));
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Output :
Database details :