java 如何修复 ERROR PACKAGE com.mysql 不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6641076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix the ERROR PACKAGE com.mysql does not exist
提问by DIPIKA PANT
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();
}
}
回答by sdolgy
As other people have put:
正如其他人所说:
- Your code is fine
- Your error is related to classpath issues
- Download the MySQL java libraries
- http://dev.mysql.com/downloads/connector/j/5.0.html
- Read the documentationhttp://dev.mysql.com/doc/refman/5.1/en/connector-j.html
- 你的代码没问题
- 您的错误与类路径问题有关
- 下载 MySQL Java 库
- http://dev.mysql.com/downloads/connector/j/5.0.html
- 阅读文档http://dev.mysql.com/doc/refman/5.1/en/connector-j.html
回答by MMM
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
并将其放在您的项目文件夹中
回答by xrcwrn
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);
}
回答by AlexR
Add appropriate jar file that contains sql driver to your classpath
将包含 sql 驱动程序的适当 jar 文件添加到您的类路径
回答by somebodyelse
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的连接周围设置一些异常捕获器,或者抛出一些异常。