• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Database connectivity issue using JAVA and MySql

 
Mayur Saparia
Greenhorn
Posts: 19
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create database connection in java using MySql. I have simple program which shows messsage went connected to mysql but the programs give error.

import java.sql.*;
import javax.sql.*;

public class MysqlConnect{

public static void main(String args[]){
String dbtime;
String dbUrl = "jdbc:mysql://your.database.domain/yourDBname";
String dbClass = "com.mysql.jdbc.Driver";
String query = "Select * FROM users";

try {

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection (dbUrl);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
} //end while

con.close();
} //end try

catch(ClassNotFoundException e) {
e.printStackTrace();
}

catch(SQLException e) {
e.printStackTrace();
}

} //end main

} //end class


Error are
run:
MySQL Connect Example.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at MysqlConnect.main(MysqlConnect.java:13)
BUILD SUCCESSFUL (total time: 1 second)

please suggest me tutorial or help me to get the connectivity. I have read a tutorial where it says i need Jconnector i have downloaded that but i dont know how to use it OR where to place that JAR file.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a JDBC tutorial. It is good.
For the MySQL connector, google for the MySQL handbook and there is a chapter about using the connector.
Don’t try it from a Swing application. Get it running at the command line and then put a Swing GUI on top of it.
 
rohan sethi
Ranch Hand
Posts: 34
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in your JDBC driver's path.Code is fine.Are you using an IDE?
 
K. Tsang
Bartender
Posts: 3404
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code, the lines

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection (dbUrl);

should be

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection (dbUrl, username, password);

And of course will need the JDBC driver in the classpath in order to run/compile.
 
Mayur Saparia
Greenhorn
Posts: 19
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everybody i could solve my problem with the help of all the solutions you posted for me.
Looking forward to come up with more question soon.
Once again thank you.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:should be

Class.forName("com.mysql.jdbc.Driver").newInstance();

That used to be the case for some older versions, but the current MySQL driver has been improved to register itself during class loading instead of initializing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic