How to connect to mysql in MapReduce temporarily?

How do I connect Java (NetBeans) to MySQL using JDBC?

  • My code is package voterid; import java.sql.*; public class voter { void vote() { String url = "jdbc:mysql://localhost:3306/"; String dbName = "vote"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password; password = "root"; try { Class.forName(driver).newInstance(); Connection conn = DriverManager.getConnection(url+dbName,userName,password); Statement st = conn.createStatement(); ResultSet res = st.executeQuery("SELECT * FROM voter"); while (res.next()) { String id = res.getString("voterid"); String msg = res.getString("fname"); System.out.println(id + "\t" + msg); } conn.close(); st.close();res.close(); } catch (Exception e) { e.printStackTrace(); } } } and the main function is package voterid; public class VoterID { public static void main(String[] args) { voter v=new voter(); v.vote(); } } and the error is java.lang.ClassNotFoundException: com.mysql.jdbc.Driver  at http://java.net.URLClassLoader$1.run(URLClassLoader.java:366)  at http://java.net.URLClassLoader$1.run(URLClassLoader.java:355)  at java.security.AccessController.doPrivileged(Native Method)  at http://java.net.URLClassLoader.findClass(URLClassLoader.java:354)  at java.lang.ClassLoader.loadClass(ClassLoader.java:425)  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)  at java.lang.ClassLoader.loadClass(ClassLoader.java:358)  at java.lang.Class.forName0(Native Method)  at java.lang.Class.forName(Class.java:190)  at voterid.voter.vote(voter.java:13)  at voterid.VoterID.main(VoterID.java:5) P.S.:- I have tried various methods given on various blogs but nothing has worked ant the connector that I am having is in MSI installer format as provided on the MySQL website

  • Answer:

    My standby advice for Java programmers is:90% of all Java problems are due to your CLASSPATH not being set right.You need the jar file for the https://dev.mysql.com/downloads/connector/j/This is the software that contains the JDBC driver. You say you have the MSI installer, but Java can't read an MSI. It's just a packaging format. You have to execute the MSI to install the software contained within it.Then you need to make sure you add that jar to your CLASSPATH when you invoke your Java application. As a Java programmer, you must understand what a CLASSPATH is and how to set it. This should be explained in any book or tutorial on Java.

Bill Karwin at Quora Visit the source

Was this solution helpful to you?

Other answers

Download the mysql-connector jar and add it to the project as an external library, code seems to be fine! Its just the jar.That's one of the reason for using spring framework, maven/gradel, they download depended jars all by themselves

Hiresh Trivedi

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.