Results 1 to 4 of 4
  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default connecting to a mysql db

    I am trying to connect to a databse. here is my code so far:

    Java Code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class Connect2 {
    
    	public static void main(String args[]) {
    		String dbtime;
    		String dbUrl = "jdbc:mysql:/dbnumber.db.1and1.com/uploader";
    		String dbClass = "com.mysql.jdbc.Driver";
    		String query = "Select * FROM members";
    
    		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
    i am getting this error:

    Java Code:
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    	at java.net.URLClassLoader$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at java.lang.Class.forName0(Native Method)
    	at java.lang.Class.forName(Unknown Source)
    	at Connect2.main(Connect2.java:17)
    I am familiar with connecting to mysql via php, but not java.

    thanks in advance.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: connecting to a mysql db

    That driver is most likely stored in a .jar file somewhere; add that .jar file to your classpath.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default Re: connecting to a mysql db

    Check out my tutorial in youtube: "Lesson 8. Connecting Java to MySql ODBC easiest way"

  4. #4
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: connecting to a mysql db

    I went through your tutorial online, and I get this error: Kamalian java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

    (sorry, ignore the attached image)
    Attached Thumbnails Attached Thumbnails connecting to a mysql db-graphic.png  
    Last edited by droidus; 10-30-2011 at 02:13 AM.

Similar Threads

  1. Connecting Mysql with J2ME
    By razmyasdfg in forum CLDC and MIDP
    Replies: 2
    Last Post: 05-22-2011, 11:40 PM
  2. connecting mysql with netbeans
    By angelia in forum New To Java
    Replies: 3
    Last Post: 05-15-2011, 12:30 PM
  3. Error while connecting to mysql server
    By Anandt88 in forum JDBC
    Replies: 1
    Last Post: 08-16-2010, 03:37 PM
  4. Connecting a MySQL DB through Eclipse
    By mitty in forum New To Java
    Replies: 3
    Last Post: 02-01-2010, 10:22 AM
  5. connecting to mysql database
    By javagal in forum NetBeans
    Replies: 2
    Last Post: 08-04-2007, 01:36 PM

Posting Permissions

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