java在EC2实例上安装Mysql驱动程序?
我在AmazonEC2上准备了一个Unix实例。我已经安装了mysql在3306端口上运行。(注意:不是AWS的rds数据库)
我已经在Mysql中创建了EmpDB数据库,并创建了Employee表。我已经编写了一个java程序在表中插入一行
代码如下:
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();
}
}
当我通过“java测试”执行它时,出现以下错误:
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
如何在Unix实例上安装mysql连接器
# 1 楼答案
将驱动程序与我的
Test.java
文件放在同一个文件夹中,并对其进行编译,生成Test.class
所以在这个文件夹中
我写下:
如果不使用任何IDE,可以在windows中使用cmd,在linux中使用shell
# 2 楼答案
只需复制MySql连接器。jar放入Tomcat的lib文件夹,然后从webapp的lib文件夹中移除jar,然后重新运行项目
导入com。mysql。jdbc。司机