How data is insert into Database (Mysql) by console application
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.sql.ResultSet;
public class Database{
public static void main (String [] args) throws SQLException {
// object to ge input from user
Scanner ip = new Scanner (System.in);
// read values from user
System.out.println("Please enter the ID");
int Id=ip.nextInt();
System.out.println("Please enter the Name");
String name= ip.next();
// url (path and port) of database in the computer
String url="jdbc:mysql://localhost:3306/hu";
// establish the connection with the database by giving user name "root"and passphrase (password) "1122"
Connection con= DriverManager.getConnection(url,"root","1122");
// create a statement
Statement st= con.createStatement();
// insert a query to add the data into the database
int rs=st.executeUpdate("insert into hu.std (rNo,Name) values (' "+Id+" ',' "+ name+ " ');");
// message for user
System.out.println("New data is enter");
// the connection is close for the protection of the databse
con.close();
}
}
Comments
Post a Comment