【问题标题】:Connect Xampp to Netbeans IDE (8.0.2)将 Xampp 连接到 Netbeans IDE (8.0.2)
【发布时间】:2021-04-18 09:23:09
【问题描述】:

您也可以查看图片中的库和代码(链接在句末)---

错误是java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver。我已经有了mysql-connector-java-5.1.23-bin,它已经粘贴在我的Webserver 的lib 目录中。下面是代码。

package databaseinsertexample;

import java.sql.*;
import java.sql.DriverManager;

public class DatabaseInsertExample {

    public static void main(String[] args) {
        try
        {
         Class.forName("com.mysql.cj.jdbc.Driver");
         Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/myteam","root","");
         String sql="insert into teams values(?,?,?)";
         PreparedStatement stmt=con.prepareStatement(sql);
         stmt.setString(1,"India");
         stmt.setString(2, "Niraj");
         stmt.setInt(3, 100);
         stmt.execute();
         con.close();
        }  catch(Exception e)
        {
          System.out.println(e);
        }    
    }
    
}

在 XAMPP 的 Localhost 站点上,数据库已正确创建,但是当我通过 Netbeans 在我的 Java 文件上执行程序时,它并没有反映在数据库上。请帮助我是这个编程世界的新手,我在 YouTube 上查看了仍然无法清除我的这个疑问。

【问题讨论】:

    标签: java


    【解决方案1】:

    像这样更改你的 forName 类。

    Class.forName("com.mysql.jdbc.Driver");
    

    另外请参考这个问题 - How to connect XAMPP MySQL local DB using JDBC?

    【讨论】:

    • 感谢您帮助@Yasas Naveen。代码正确执行,也感谢您的链接!
    • 欢迎您@NirajVora。如果您的问题解决了,请接受答案。
    猜你喜欢
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2017-12-28
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    相关资源
    最近更新 更多