将 Xampp 中的 mysql 用于 java jdbc 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13611352/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:36:31  来源:igfitidea点击:

Using mysql from Xampp for java jdbc program

mysqlnetbeansjdbcconnectivityxampp

提问by dhpratik

I have Xampp installed on my pc with mysql database. now i wish to use this mysql database for my java JDBC program. For that i have writen following program.

我在我的电脑上安装了 Xampp 和 mysql 数据库。现在我希望将这个 mysql 数据库用于我的 java JDBC 程序。为此,我编写了以下程序。

package mysqltype4driverdemo;

import java.sql.*;
import java.util.*;


public class MysqlType4DriverDemo {

    public static void main(String[] args)throws SQLException {
        String url="jdbc:mysql://localhost:3306/mysql";
        Properties prop=new Properties();
        prop.setProperty("user","root");
        prop.setProperty("password","");
        Driver d=new com.mysql.jdbc.Driver();
        Connection con = d.connect(url,prop);
        if(con==null)   {
            System.out.println("connection failed");
            return;
        }
        DatabaseMetaData dm =con.getMetaData();
        String dbversion=dm.getDatabaseProductVersion();
        String dbname=dm.getDatabaseProductName();
        System.out.println("name:"+dbname);
        System.out.println("version:"+dbversion);

    }
}

but it says "package com.mysql.jdbc" does not exists. P.S. : i am using netbeans 7.2.x IDE on windows XP platform

但它说“包com.mysql.jdbc”不存在。PS:我在 Windows XP 平台上使用 netbeans 7.2.x IDE

回答by RobertB

It appears that you might have tried putting the library on the global CLASSPATH. For Netbeans projects, that's not quite right. You need to add the appropriate library(ies) to the project using Netbeans's library facility.

看来您可能已经尝试将库放在全局 CLASSPATH 上。对于 Netbeans 项目,这不太正确。您需要使用 Netbeans 的库工具将适当的库添加到项目中。

  1. Right-clickon the project's root node in the Projectstab.
  2. In the pop-up context menu, click on Properties(on the bottom of the menu).
  3. Click on Librariesunder Categories:. You should see the following screen: Netbeans Project Properties Dialog: Libraries
  4. Click on the Add Library...button.
  5. Under Global Librariesclick on MySQL JDBC Driverand then click the Add Librarybutton.
  6. Click on OK.
  1. 项目选项卡中右键单击项目的根节点。
  2. 在弹出的上下文菜单中,单击“属性”(在菜单底部)。
  3. 单击类别下的。您应该看到以下屏幕: Netbeans 项目属性对话框:库
  4. 单击添加库...按钮。
  5. 全局库下单击MySQL JDBC 驱动程序,然后单击添加库按钮。
  6. 单击“确定”

You should be good to go.

你应该很高兴去。

If you need a specific version of the driver, you can download it, and then after clicking on Add Library...you can click on Create...to add the downloaded version to your library repository. You'd then remove the default JDBC Driver from the project, and add the library containing the specific version.

如果您需要特定版本的驱动程序,您可以下载它,然后在单击Add Library... 后,您可以单击Create...将下载的版本添加到您的库存储库中。然后从项目中删除默认的 JDBC 驱动程序,并添加包含特定版本的库。

I tried this out myself using your code and a newly-created project. No additional imports needed, and the default driver included with the Netbeans distro should be good enough unless you need a specific version for your project.

我使用您的代码和新创建的项目自己尝试了这一点。不需要额外的导入,并且 Netbeans 发行版中包含的默认驱动程序应该足够好,除非您的项目需要特定版本。

回答by Robert H

You need to download the JDBC driver for mysql from here.

您需要从这里下载 mysql 的 JDBC 驱动程序。

Once you download it, add the jar to your classpath and you should be good to go.

下载后,将 jar 添加到您的类路径中,您就可以开始使用了。

回答by Vaibhav Joshi

Alternatively, once you have downloaded the driver, you can paste it in you java installation at ...jre\lib\ext\

或者,一旦您下载了驱动程序,您可以将其粘贴到您的 java 安装中...jre\lib\ext\

Then it would be available for all your projects with the default JRE system library which you can verify when you create a new project.

然后它将可用于具有默认 JRE 系统库的所有项目,您可以在创建新项目时进行验证。