Nicky
Nicky

Reputation: 1075

Install Mysql driver on EC2 instance?

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?

Upvotes: 0

Views: 2394

Answers (2)

Xiaobing Mi
Xiaobing Mi

Reputation: 130

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.

Upvotes: 0

Xiaobing Mi
Xiaobing Mi

Reputation: 130

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;

Upvotes: 1

Related Questions