[英]How to fix the ERROR PACKAGE com.mysql does not exist
How to fix this program?如何修复这个程序?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
class MySqlData {
public static void main(String args[]) throws Exception {
DriverManager.registerDriver(new com.mysql.jdbcDriver());
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test/user=root&password=merimaa");
Statement stmt = con.createStatement();
ResultSet res=stmt.executeQuery("Select * from employee");
while (res.next()) {
System.out.println(res.getString("employee_name"));
}
con.close();
}
}
As other people have put:正如其他人所说:
You'll need to download the MySQL Connector.您需要下载 MySQL 连接器。
Download it from here: https://dev.mysql.com/downloads/connector/j/从这里下载: https://dev.mysql.com/downloads/connector/j/
and drop it in your project folder并将其放入您的项目文件夹中
Add Jar file to WEB-INF/lib.将 Jar 文件添加到 WEB-INF/lib。
Use below下面使用
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
try {
String connectionURL = "jdbc:mysql://host/db";
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "username", "password");
if(!connection.isClosed())
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}catch(Exception ex){
out.println("Unable to connect to database"+ex);
}
https://stackoverflow.com/a/13506117/876739 https://stackoverflow.com/a/13506117/876739
Add appropriate jar file that contains sql driver to your classpath将包含 sql 驱动程序的适当 jar 文件添加到您的类路径
First you need to download a mysql driver, next set up some exception catchers arround the connection with the mysql, or throw some exception.首先你需要下载一个 mysql 驱动程序,然后在与 mysql 的连接周围设置一些异常捕获器,或者抛出一些异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.