0

I have a Unix Instance ready on Amazon EC2 .I have installed mysql in it which is runnig on 3306 port. ( Note: It is not rds database from AWS)

I have created EmpDB databse in Mysql and have created Employee table. And I have written a java program to insert a row in Table.

Below is the code:

import java.sql.*;
import java.util.Calendar;
public class Test{
  public static void main(String[] args) {
    try{
      String myDriver = "com.mysql.jdbc.Driver";
      String myUrl = "jdbc:mysql://localhost:3306/TaskDB";
      Class.forName(myDriver);
      Connection conn = DriverManager.getConnection(myUrl, "root", "root");
      String query = " insert into employee (id,name,dept,salary)"
                     + " values (?, ?, ?, ?)";
      PreparedStatement preparedStmt = conn.prepareStatement(query);
      preparedStmt.setInt (1, 600);
      preparedStmt.setString (2, "Rubble");
      preparedStmt.setString(3, "startDate");
      preparedStmt.setInt(4, 5000);
      preparedStmt.execute();
      conn.close();
     } catch (Exception e){
         System.err.println("Got an exception!");
         e.printStackTrace();
     }
 }

When I am executing it via "java Test", getting below error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:359)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:348)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net

How I can install mysql connector on my Unix Instance?

Nicky
  • 1,025
  • 3
  • 15
  • 29
  • 1
    Possible duplicate of [java.lang.ClassNotFoundException:com.mysql.jdbc.Driver](http://stackoverflow.com/questions/15827547/java-lang-classnotfoundexceptioncom-mysql-jdbc-driver) – xitter Dec 18 '16 at 00:41

2 Answers2

1

Just copy the MySql-Connector.jar into Tomcat's lib folder, and then remove the jar from the webapp's lib folder, and then, re run the project.

import com.mysql.jdbc.Driver;

Xiaobing Mi
  • 102
  • 5
0

Put the driver in the same folder as my Test.java file, and compile it, resulting in Test.class.

So in this folder have

Test.class
mysql-connector-java-5.1.14-bin.jar

and I write this:

java -cp .;mysql-connector-java-5.1.14-bin.jar Test

This way if you not use any IDE just cmd in windows or shell in linux.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Xiaobing Mi
  • 102
  • 5