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();
}
}
}
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();
}
}
}