Results 1 to 3 of 3
  1. #1
    ranvir is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Unhappy how to connect java to mysql?

    hello friend this is ranvir,
    i'm working on simple java programm to connect mysql database, but showing error after compiled,
    this is my programm....

    MysqlConnect.java
    ###################

    Java Code:
    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 = "nik";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "ranvir";
        try {
          Class.forName(driver).newInstance();
          conn = DriverManager.getConnection(url+dbName,userName,password);
          System.out.println("Connected to the database");
          conn.close();
          System.out.println("Disconnected from database");
        } catch (Exception e) {
        }
      }
    }
    after compilation showing some error
    ################################
    error(when execute)
    ----------------
    Database creation example!
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at jdbc1.CreateDatabase.main(CreateDatabase.java:11)
    BUILD SUCCESSFUL (total time: 1 second),

    please suggest me ,what is possiblity of error,how to solve it?
    Last edited by Eranga; 12-09-2010 at 03:04 AM. Reason: code tags added

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    Please use code tags, [CODE][/CODE], when posting codes

    I am not sure if this is the code you are using but your password became pa ssword in getConnection method.
    String password = "ranvir";
    ...
    conn = DriverManager.getConnection(url+dbName,userName,pa ssword);


    Also, your try..catch statement does not print the error( e.printStackTrace(); ).
    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) {
    }
    and yet you were able to tell us the problem base on printed stack trace.

    It will be hard for us to know the actual/real problem if you give us code with different output.

    About the error, did you already add the library of MySql on your class path?
    Last edited by mine0926; 12-09-2010 at 01:22 AM.

  3. #3
    Javaxcoder is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Smile Do you have the Jconnector for my sql?

    You need driver for connecting a java program to MySQL.
    The codes wont work if you dont have Jconnector installed...
    I've explained the procedure to connect java pgms to mysql on a post in my blog.


    http://javaxcoder.co.cc

Similar Threads

  1. Eclipse Mysql connect
    By jyotirmoy_deb in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-04-2010, 03:57 PM
  2. how to connect MySql with JSP
    By sauravsinha in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-13-2010, 01:50 PM
  3. Can NOT connect to Mysql
    By zed420 in forum New To Java
    Replies: 3
    Last Post: 12-18-2009, 08:26 PM
  4. connect java with mysql in rhel5
    By niraj.vara in forum New To Java
    Replies: 0
    Last Post: 07-17-2009, 09:23 AM
  5. Replies: 2
    Last Post: 06-12-2008, 07:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •