Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Using mysql in Java on ubuntu

    Hi,

    I am new to Java programming language. I am studying from these from Java server programming from Black book.

    I am not getting this example basically the first part for registering the driver object with DriverManager class.

    Java Code:
    package com.kogent.jdbc;
    
    import java.sql.CallableStatement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    
    public class CallableStatementEx1 {
    	private static final String url = "jdbc:mysql://localhost";
    	private static final String user = "root";
    	private static final String password = "v";
    	
    	public static void main(String[] args) throws Exception {
    		Class.forName("com.mysql.jdbc.Driver").newInstance();
    		Connection con = DriverManager.getConnection(url, user, password);
    		
    		// Get CallableStatement
    		CallableStatement cs = con.prepareCall("{call createAccount (?, ?, ?, ?, ?, ?,)}");
    		
    		// Set IN parameters
    		cs.setInt(1, 103);
    		cs.setInt(2, 9);
    		cs.setString(3, "Vivek");
    		cs.setInt(4, 10000);
    		cs.setString(5, "Delhi");
    		cs.setInt(6, 965068);
    		
    		// Executing stored procedure
    		cs.execute();
    		System.out.println("Account created");
    		
    		// Close the connection
    		con.close();
    	}
    }
    when running this program. I am getting the below exceptions.

    Java Code:
    Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    	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 com.kogent.jdbc.CallableStatementEx1.main(CallableStatementEx1.java:13)
    If have problem in understanding below lines of the program.
    url = "jdbc:mysql://localhost";
    Class.forName("com.mysql.jdbc.Driver").newInstance ();
    Any suggestion will be greatly helpful.

    Thanks!
    Last edited by dearvivekkumar; 11-30-2011 at 03:47 AM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Using mysql in Java on ubuntu

    You have to add the mysql driver (jdbc driver) to your build path / classpath :-)
    MySQL :: Download Connector/J

  3. #3
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: Using mysql in Java on ubuntu

    I have already added in the CLASSPATH env variable.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Using mysql in Java on ubuntu

    Never use the environment variable to hold your Java classpath.
    Use the -cp switch of either java (in this case) or javac to define the classpath, or define it in whatever IDE you are using.

    How are you running this?

Similar Threads

  1. Can't Install Ubuntu Natty Narwhal Java 7 JDK
    By kayve in forum New To Java
    Replies: 0
    Last Post: 09-16-2011, 09:45 PM
  2. Ubuntu, run executable file in java
    By felipin85 in forum New To Java
    Replies: 0
    Last Post: 05-11-2011, 12:31 PM
  3. Java TTS on Ubuntu with Festival
    By Desh Banks in forum Advanced Java
    Replies: 2
    Last Post: 01-06-2011, 12:09 AM
  4. setting classpath for java in ubuntu
    By kulangotski in forum Java Servlet
    Replies: 0
    Last Post: 01-03-2011, 02:18 AM
  5. mysql classpath ubuntu problem
    By munish in forum JDBC
    Replies: 18
    Last Post: 08-22-2010, 01:55 AM

Tags for this Thread

Posting Permissions

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