Insert values into table Using Java
/* A Sample program to Insert values to a table Using Java*/
import java.sql.*;
import java.util.*;
public class Insert
{
public static void main(String args[]) {
try {
/* Test loading driver */
Hashtable members = new Hashtable();
// Load the JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Establish a connection
String url = "jdbc:mysql://localhost/test"; //here "test" is the name of the database in use
Connection connection = DriverManager.getConnection( url, "root", "kris" ); //root and password is name
/* Create a statement*/
Statement statement = connection.createStatement();
/* Execute a statement for selecting values from database here details is the tables name in database "test"*/
String name[]={"Siva","Phanindra","Vemuri"};
String ID[]={"1000","9000","1232"};
/*process to insert values into database*/
for(int i=0;i<name.length;i++){
String INSERT = "insert into phani(name,ID)values ('" + name[i] + "','" +
ID[i] + "')";
int j = statement.executeUpdate(INSERT);
}
System.out.println("--------Values Inserted into table Successfully----------");
}
catch( Exception x ) {
x.printStackTrace();
}
}
}
/* Prerequisites you need to have Mysql installed in your system with a data base named "test"
and with username "root" and password "name"*/
/* Javac Insert.java */
/* Java Insert */
import java.sql.*;
import java.util.*;
public class Insert
{
public static void main(String args[]) {
try {
/* Test loading driver */
Hashtable members = new Hashtable();
// Load the JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Establish a connection
String url = "jdbc:mysql://localhost/test"; //here "test" is the name of the database in use
Connection connection = DriverManager.getConnection( url, "root", "kris" ); //root and password is name
/* Create a statement*/
Statement statement = connection.createStatement();
/* Execute a statement for selecting values from database here details is the tables name in database "test"*/
String name[]={"Siva","Phanindra","Vemuri"};
String ID[]={"1000","9000","1232"};
/*process to insert values into database*/
for(int i=0;i<name.length;i++){
String INSERT = "insert into phani(name,ID)values ('" + name[i] + "','" +
ID[i] + "')";
int j = statement.executeUpdate(INSERT);
}
System.out.println("--------Values Inserted into table Successfully----------");
}
catch( Exception x ) {
x.printStackTrace();
}
}
}
/* Prerequisites you need to have Mysql installed in your system with a data base named "test"
and with username "root" and password "name"*/
/* Javac Insert.java */
/* Java Insert */