Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics 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 September 10th, 2008, 02:15 PM
ruzdi
Guest
 
Posts: n/a
Default Java MySql Connection

Hi Friends,
Can you please tell me the procedure of java and mysql connection using jdbc driver . Please help me.



 
Old September 10th, 2008, 03:30 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default


Code:
String url = "jdbc:mysql://SERVERNAME:3306/STARTINGDATABASENAME";
String user = "USERNAME";
String pw = "PASSWORD";
Connection dbConn = DriverManager.getConnection( url, user, pw );
Replace the stuff in RED with your own values. (You might need to change the port number, 3306, but it's unlikely.)

There are better ways to make connections, but this is the simplest.
 
Old September 11th, 2008, 01:24 PM
ruzdi
Guest
 
Posts: n/a
Default

Hello Old Pedant , Is there need anything setup like Enviornment variable or file path location .

 
Old September 11th, 2008, 02:04 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Nope. You do need for the given SERVERNAME to be accessible to the machine where you are running th Java code. And you have to set MySQL to allow remote connections if it's not on the same server. But those are all MySQL admin issues, nothing directly to do with Java.
 
Old September 13th, 2008, 01:08 PM
ruzdi
Guest
 
Posts: n/a
Default

Hello dear Old Pedant i have done following thins-----------

1> i have started mysql server whose SERVERNAME="MySQL"
    password="rushdi" user="root" port="3306" database-name="test"

2> i execute the following code ---------------

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MissingDbConnection {

    //protected String ur1;
    protected Connection connect;

    protected void SqlConnection(String userName,String password){

        try{

            String url = "jdbc:mysql://MySQL:3306/test";
            //String url = "jdbc:mysql:///test";
            //String url = "jdbc:mysql://localhost:3306/test";
            Class.forName("com.mysql.jdbc.Driver").newInstance ();
            connect= DriverManager.getConnection( url, userName, password );
            if(!connect.isClosed()){
                System.out.println("Successfully connected to "+"MySQL server using TCP/IP...");
            }
        }catch(Exception ex){
        System.err.println("Exception: " + ex.getMessage());
        }finally {
            try {
                if(connect != null)
                    connect.close();
            }catch(SQLException e) {
                //---------------------------
            }

        }

    }

    public Connection connectionString(){
        return connect;
    }

    public static void main(String args[]) {
        MissingDbConnection missingdbConnection = new MissingDbConnection();
        missingdbConnection.SqlConnection("root", "rushdi");

    }

}

------------------after that i got

Exception: com.mysql.jdbc.Driver

so , what is the problem is there anything missing;

3> one more thing i have tried all the three url("String url" in program) ;

 
Old September 13th, 2008, 04:30 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Can't tell from just that one line of error, but quite possibly you don't have the library imported and/or available on your classpath.

I get a similar error when I debug from Eclipse and forget to add a reference to the JDBC drivers in my project.
 
Old September 14th, 2008, 02:13 PM
ruzdi
Guest
 
Posts: n/a
Default

Dear Old Pedant ,
Can you please tell me what library should be imported and what referance to the JDBC driver should be created and How .
please tell me the steps as detail as possible

 
Old September 14th, 2008, 02:36 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

One of the problems with using a tool (Eclipse, in this case) to help write code is that you forget all the steps you used to use before you had the tool.

I don't remember them right now, and the computer I am on doesn't have the files handy. But I will look around tomorrow and see if I can find all the JAR files you need. No promises, but I will look.
 
Old September 15th, 2008, 02:56 PM
ruzdi
Guest
 
Posts: n/a
Default

Dear Old Pedant ,
1> I am using netbean ide

2> One more thing after adding printStackTrace(); i found the following log

Exception: com.mysql.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader$1.run(URLClassLoader.java: 200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:276)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
        at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:319)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:169)
        at MissingRuzdi.MissingDbConnection.SqlConnection(Mis singDbConnection.java:28)
        at MissingRuzdi.MissingDbConnection.main(MissingDbCon nection.java:55)
BUILD SUCCESSFUL (total time: 0 seconds)




The Following User Says Thank You to For This Useful Post:
SKYDOS (December 16th, 2008)
 
Old September 15th, 2008, 03:21 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Forgot to ask what operating system you are using.

If you are using Windows, you might want to look at the steps given here:
http://www.stardeveloper.com/article...3090201&page=1

and maybe read this:
http://forums.sun.com/thread.jspa?th...sageID=2503366

If not Windows, then what?
The Following User Says Thank You to Old Pedant For This Useful Post:
SKYDOS (December 16th, 2008)





Similar Threads
Thread Thread Starter Forum Replies Last Post
C# and mySQL Connection naifwonder C# 5 March 8th, 2008 01:52 PM
making an mysql connection Zoe-Alyssa Dreamweaver (all versions) 0 May 19th, 2006 02:16 PM
java/jsp and MySQL harshika Java Databases 2 August 2nd, 2005 02:07 AM
use ODBCDataAdapter Connection MySQL kofallen Classic ASP Databases 2 April 19th, 2005 03:02 PM
Mysql connection catab C# 4 December 7th, 2003 11:25 AM





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