import java.sql.*;
public class DataBase
{
public static void main(String args[])
{
Connection con = null;
Statement stmt = null;
ResultSet res = null;
try
{
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Class.forName("org.gjt.mm.mysql.Driver");
Class.forName("com.mysql.jdbc.Driver");
/* con = DriverManager.getConnection("jdbc:odbc:rams","scot t","tiger");*/
/* con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?user=root&password="); */
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","");
stmt = con.createStatement();
res = stmt.executeQuery("select * from user");
while(res.next())
{
System.out.println(res.getString(1));
}
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
try
{
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
e:\rama\java>set classpath=%classpath%;mysql-connector-java-3.0.15-ga-bin.jar
e:\rama\java>javac DataBase.java
e:\rama\java>java DataBase :)
I am using mysql-connector-java-3.0.15-ga-bin.jar. Placed the jar file in the same dir of java file.
Take care about MySQL's user & password.
Still if u r getting problem, u have to check the folder structure in jar file (by unzip it) and follow the same in the Class.forName().
|