Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

database connectivity error

 
midhun raj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using mysql as database and netbeans as IDE ..
I tried this small code to get database connectivity
[code=java
package database;

/**
*
* @author midhun
*/

import java.sql.*;

public class JDBCSAMPLE {

public static void main( String args[]) {

String connectionURL = "jdbc:mysql://localhost:3306/cmgmt";
// Change the connection string according to your db, ip, username and password

try {

// Load the Driver class.
Class.forName("com.mysql.jdbc.Drive");
// If you are using any other database then load the right driver here.

//Create the connection using the static getConnection method
Connection con = DriverManager.getConnection (connectionURL);

//Create a Statement class to execute the SQL statement
Statement stmt = con.createStatement();

//Execute the SQL statement and get the results in a Resultset
ResultSet rs = stmt.executeQuery("select * from regist;");

// Iterate through the ResultSet, displaying two values
// for each row using the getString method

while (rs.next())
System.out.println("Name= " + rs.getString("f_n") + " sex= " + rs.getString("sex")+"place="+rs.getString("place"));
}
catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}

}
}

][/code]

But i am getting the error when i run the fie error is

init:
deps-module-jar:
deps-ear-jar:
deps-jar:
compile-single:
run-main:
java.lang.ClassNotFoundException: com.mysql.jdbc.Drive
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at database.JDBCSAMPLE.main(JDBCSAMPLE.java:25)
BUILD SUCCESSFUL (total time: 2 seconds)

Any guess how to solve this problem..
 
Pradyumna Sahoo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Try this way buddy

 
Wayan Saryada
Ranch Hand
Posts: 105
IntelliJ IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
midhun raj wrote:



There was a typo error in your driver class name. If you are using the latest JDBC driver (JDBC 4.x) you don't have to use Class.forName() anymore to load the driver. The DriverManager will locate the suitable driver for you.
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 135
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think

line is generating error because if you want to connect MySQL and java you have to load mysql jdbc driver. not

so try with following code


Hope this was helpful.
Regards!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic