Wrox Programmer Forums
|
Java Databases Discussion specific to working with Java Databases. For other Java topics, please see related Java forums. For database discussions not specific to Java, please see the Database category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 1st, 2006, 07:54 AM
Registered User
 
Join Date: Apr 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default jdbc-mysql

Hi all,
I am very new to jdbc:mysql connevtivity.
I am using j2sdk1.4.2_07 and mysql.
Please anyone write me how to connect java and mysql

venkatesh
 
Old April 13th, 2006, 02:20 AM
Authorized User
 
Join Date: Mar 2006
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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().






Similar Threads
Thread Thread Starter Forum Replies Last Post
jdbc:mysql connectivity kvenk111 Pro JSP 2 April 21st, 2006 02:25 AM
mysql + JDBC + Linux silver_cuts Java Databases 2 March 23rd, 2006 09:28 AM
mysql+JDBC over LAN bamboat_3 Java Databases 1 April 1st, 2005 04:14 AM
Cannot load JDBC driver class with com.mysql.jdbc. dzis Apache Tomcat 0 January 29th, 2004 11:57 AM
Cannot load JDBC driver class with com.mysql.jdbc. dzis MySQL 0 January 29th, 2004 11:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.