1.import sql package
2.Give all the Supporting class in class.forName
3.Give Ipaddress or Computer Name in dburl or in getConnection
4.Give Username and Password in getConnection
5.Open the Connection using createStatement
6.Execute the query String using executeQuery Statement
7.Fetch the Record one by one Using ResultSet
8..mysqldemo is a schema name
9.Close the Connection
import java.io.*;
import java.sql.*;
public class MysqlConnection
{
public static String dburl=”jdbc:mysql://localhost/mysqldemo”;
public static String username=”root”;
public static String pwd=”root”;
public static Connection con=null;
public static Statement stmt=null;
public static ResultSet rs=null;
public static void main(String[] args)
{
try {
Class.forName(”org.gjt.mm.mysql.Driver”);
(or)
Class.forName(”com.mysql.jdbc.Driver”);//use this
con=DriverManager.getConnection(dburl,username,pwd);
stmt=con.createStatement();
rs=stmt.executeQuery(”SELECT * FROM demo”);
while(rs.next())
{
String name=rs.getString(1);
}
}
catch(SQLException sqlExcp)
{
sqlExcp.printStackTrace();
}
}
}
Popularity: 1% [?]

November 10th, 2008
admin
Posted in
Tags: 
