« Home | Program to Retrive values from Mysql Using Java » | Java Mysql Connectivity » | Program to Delete Files in a Given Directory » | Program to Write to a file » | Program to Rename Files in a given Directory » 

Thursday, March 09, 2006 

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 */

About me

  • I'm phanindra
  • From Hyderabad, Andhra Pradesh, India
  • An Alumini of JNTU Hyd into happening IT industry...
My profile

Visitors
Bloggeries Blog Directory BlogRankings.com singapore blog directory blog search directory
Google
 
Web conceptoftheday.blospot.com