簡體   English   中英

如何修復錯誤 PACKAGE com.mysql 不存在

[英]How to fix the ERROR PACKAGE com.mysql does not exist

如何修復這個程序?

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();
    }
}

正如其他人所說:

您需要下載 MySQL 連接器。

從這里下載: https://dev.mysql.com/downloads/connector/j/

並將其放入您的項目文件夾中

將 Jar 文件添加到 WEB-INF/lib。

下面使用

<%@ 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

將包含 sql 驅動程序的適當 jar 文件添加到您的類路徑

首先你需要下載一個 mysql 驅動程序,然后在與 mysql 的連接周圍設置一些異常捕獲器,或者拋出一些異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM