« Home | 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 » 

Wednesday, March 08, 2006 

Program to Retrive values from Mysql Using Java

/* A Sample program to connect to Mysql Using Java*/

import java.sql.*;

public class Test
{
public static void main(String args[]) {
try {
/* Test loading driver */

Hashtable members = new Hashtable();
String driver = "com.mysql.jdbc.Driver";
System.out.println( "----->loading driver:------->" );
Class.forName( driver );

/* Test the connection Here test is the name of the database you want to connect to*/
String url = "jdbc:mysql://localhost/test";

/*root is the user name and kris is the password of MYSql*/
DriverManager.getConnection( url, "root", "kris" );
System.out.println("connection established");

/* Create a statement*/
Statement statement = connection.createStatement();


/* Execute a statement for selecting values from database here details is the tables name in database "test"*/
ResultSet resultSet = statement.executeQuery("select * from details");
while (resultSet.next()){
members.put(resultSet.getString(1),resultSet.getString(2));
System.out.println(resultSet.getString(1) + "\t" +
resultSet.getString(2) );

}
catch( Exception x ) {
x.printStackTrace();
}
}
}

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