JDBC Connectivity using Eclipse
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.*;
public class mysqlconn {
public static void main(String args[]){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/demo","root","root");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2));
int i=17;
String name1="smita";
int did=4;
int credit =2000;
InputStreamReader r1=new InputStreamReader(System.in);
BufferedReader br1=new BufferedReader(r1);
System.out.println("Enter your id");
try {
name1=br1.readLine();
}catch(Exception E) {}
i=Integer.parseInt(name1);
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
System.out.println("Enter your name");
try {
name1=br.readLine();
}catch(Exception E) {}
String sql = "INSERT INTO emp VALUES ("+i+","+"'"+name1+"'"+","+did+","+credit+")";
PreparedStatement statement = con.prepareStatement(sql);
int rowsInserted = statement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("A new user was inserted successfully!");
}
sql="UPDATE emp SET ename='Gaurav' WHERE eid=4";
statement = con.prepareStatement(sql);
int rowsUpdated = statement.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("An existing user was updated successfully!");
}
InputStreamReader r4=new InputStreamReader(System.in);
BufferedReader br4=new BufferedReader(r4);
System.out.println("Enter id of employee which is to be deleted: ");
try {
name1=br4.readLine();
}catch(Exception E) {}
i=Integer.parseInt(name1);
sql = "DELETE FROM emp WHERE eid="+i;
statement = con.prepareStatement(sql);
int rowsDeleted = statement.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("A user was deleted successfully!");
}
}catch (Exception e) {}
}
}