 |
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
|
|
|

September 10th, 2008, 02:15 PM
|
|
Java MySql Connection
Hi Friends,
Can you please tell me the procedure of java and mysql connection using jdbc driver . Please help me.
|

September 10th, 2008, 03:30 PM
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
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.
|

September 11th, 2008, 01:24 PM
|
|
Hello Old Pedant , Is there need anything setup like Enviornment variable or file path location .
|

September 11th, 2008, 02:04 PM
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
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.
|

September 13th, 2008, 01:08 PM
|
|
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) ;
|

September 13th, 2008, 04:30 PM
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
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.
|

September 14th, 2008, 02:13 PM
|
|
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
|

September 14th, 2008, 02:36 PM
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
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.
|

September 15th, 2008, 02:56 PM
|
|
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:
|
|
|
 |