Results 1 to 13 of 13
Thread: no mysql connection
- 07-17-2008, 07:46 AM #1
Member
- Join Date
- Jul 2008
- Posts
- 10
- Rep Power
- 0
no mysql connection
:(my pc is installed with fedora-8,LMAP is running.
i installed netbeans 6.1 ide,the databases detected and the tables expanded
easily.project ->java deskstop->databases->jdbc:mysqldriver->compile and run well.
But when come to scripting :(from the tutorial)
import java.sql.*;
public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,pa ssword);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
System.out.println("Connection fails");
}
}
}
results :MySQL Connect Example.
Connection fails.
why?please help!!
thank you.
- 07-17-2008, 09:07 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Add a e.printStackTrace() to that catch block so that you can see why it fails.
In real life do like be told simply "No.", or do you prefer "No, because ...."? It's the same thing with that catch block. Currently you are simply saying "No." without providing any kind of explanation.
- 07-17-2008, 11:16 AM #3
Member
- Join Date
- Jul 2008
- Posts
- 31
- Rep Power
- 0
Its probably because you do not have the driver installed,
refer to eclipse/10180-connector-j-eclipse-mysql-5-0-x.htm -- That post
- 07-17-2008, 03:59 PM #4
Member
- Join Date
- Jul 2008
- Posts
- 10
- Rep Power
- 0
i had tried to add e.printStackTrace() as this
import java.sql.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "acc";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,pa ssword);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
// TODO code application logic here
}
}
and results :
init:
deps-jar:
Compiling 1 source file to C:\Documents and Settings\Chia\My Documents\NetBeansProjects\mysql\build\classes
compile:
run:
MySQL Connect Example.
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 mysql.Main.main(Main.java:28)
BUILD SUCCESSFUL (total time: 0 seconds)
netbeans comes with build-in mysql-connector-j-5.x.x.x
the java jdk is 1.6 update 2.
the above output is similar to winxp/mandriva2008.
thank you.
- 07-17-2008, 07:43 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
include the mysql jarfile on the classpath when you execute the application.Java Code:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
- 07-18-2008, 01:28 AM #6
Member
- Join Date
- Jul 2008
- Posts
- 10
- Rep Power
- 0
sorry for this silly question(as a newbie)
what is include the mysql jarfile on the classpath when you execute the application.?i really catch no ball!!!
i really appreciate for details explanation.thank you.
- 07-18-2008, 08:13 AM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
See Lesson: Common Problems (and Their Solutions) (The Java™ Tutorials > Getting Started)Java Code:java -cp <path to mysqljarfile> yourclass
- 07-18-2008, 08:17 AM #8
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
or you can simply download the mysql driver as said above and put it in your java local folder
..\Java\jdk1.6.0_05\jre\lib\ext\Mind only knows what lies near the heart, it alone sees the depth of the soul.
- 07-18-2008, 08:22 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Are sure this is works? I have try this few years back, but it doesn't work actually. Only different is at that time I used jdk 1.4
- 07-18-2008, 08:29 AM #10
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
No. It "works", but you should never place anything in the jre language extension library folder. What happens when you upgrade java? You have to copy all the jarfiles there again. What happens when you have two applications that both use the same Java version, but need different
versions of the Driver? What happens (when simply placing all jars in the same folder) two different jars have a class of the same name under the same package, but are completely incompatable? Which one will be loaded when that class is referenced? Also, why should this jar be loaded, and searched, when you start an application that has absolutely nothing to do with MySQL?
You may say this is "far-fetched", but it does happen, and more often than you might think. Some of these are, IMHO, part of the reasons
why Sun released Derby/JavaDB only as part of the JDK, rather than as a part of the JRE.
Again, never, never place anything in jre/lib/ext. That is for language extensions not third party libraries, and for good reason.
- 07-18-2008, 08:35 AM #11
Member
- Join Date
- Jul 2008
- Posts
- 10
- Rep Power
- 0
thank you for the replies.
i had checked through the forum for *classpath*.there are plenty/countless of newbie stuck with the classpath pheonomena.
i yet to try out yours suggestions ,but i sincerely the forum moderator/admin will come up short tutorial regarding this for wins/linux platform.
thank you.
- 07-19-2008, 05:32 PM #12
Member
- Join Date
- Jul 2008
- Posts
- 10
- Rep Power
- 0
i think the solution found,just putting the mysql driver to ..\java..\ext directory.
it works for wins / linux.
thank you.
- 07-19-2008, 07:57 PM #13
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Similar Threads
-
mySQL HELP!!!
By ace84 in forum New To JavaReplies: 0Last Post: 04-19-2008, 10:39 PM -
Mysql Help
By Sumendra Maharjan in forum JDBCReplies: 1Last Post: 08-08-2007, 01:19 AM -
MySql in NetBeans 5.0
By javagal in forum NetBeansReplies: 6Last Post: 07-04-2007, 02:34 PM -
servlet using MySql
By Peter in forum Java ServletReplies: 2Last Post: 07-04-2007, 01:48 PM -
JSP and MySQL
By Ed in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 07-04-2007, 05:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks