how Insert data in mysql using java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class Insert {
public static void main(String[] args) throws ClassNotFoundException {
// Class.forName(“in.tss.my.A”);
Class.forName(“com.mysql.jdbc.Driver”);
Connection con=null;
try {

con= DriverManager.getConnection(“jdbc:mysql://localhost:3306/tss_db”, “root”, “”);
Statement s= con.createStatement();
Scanner s1=new Scanner(System.in);
System.out.println(“Enter Your name”);
String name= s1.next();
System.out.println(“Enter Your Age”);
int age= s1.nextInt();

System.out.println(“Enter Your Mobile”);
String mobile= s1.next();

String sql=”insert into student(name,age,mobile) values(‘”+name+”‘,'”+age+”‘,'”+mobile+”‘)”;
int i= s.executeUpdate(sql);
if( i>0){
System.out.println(“Successfully inserted”);

}else{
System.out.println(“not inserted”);
}
} catch (SQLException ex) {
ex.printStackTrace();
}finally{
try {
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}

}

Leave a Reply

Your email address will not be published. Required fields are marked *