gpt4 book ai didi

java - 运行jdbc时出错

转载 作者:行者123 更新时间:2023-11-29 20:54:19 24 4
gpt4 key购买 nike

遵循的步骤

  • mysql工作台安装
  • Connector-J 下载并解压到程序文件中的 Mysql 文件夹已复制

    import java.sql.*;
    public class FirstExample {
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost/EMP";

    // Database credentials
    static final String USER = "username";
    static final String PASS = "password";

    public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    try{
    //STEP 2: Register JDBC driver
    Class.forName("com.mysql.jdbc.Driver");

    //STEP 3: Open a connection
    System.out.println("Connecting to database...");
    conn = DriverManager.getConnection(DB_URL,USER,PASS);

    //STEP 4: Execute a query
    System.out.println("Creating statement...");
    stmt = conn.createStatement();
    String sql;
    sql = "SELECT id, first, last, age FROM Employees";
    ResultSet rs = stmt.executeQuery(sql);

    //STEP 5: Extract data from result set
    while(rs.next()){
    //Retrieve by column name
    int id = rs.getInt("id");
    int age = rs.getInt("age");
    String first = rs.getString("first");
    String last = rs.getString("last");

    //Display values
    System.out.print("ID: " + id);
    System.out.print(", Age: " + age);
    System.out.print(", First: " + first);
    System.out.println(", Last: " + last);
    }
    //STEP 6: Clean-up environment
    rs.close();
    stmt.close();
    conn.close();
    }catch(SQLException se){
    //Handle errors for JDBC
    se.printStackTrace();
    }catch(Exception e){
    //Handle errors for Class.forName
    e.printStackTrace();
    }finally{
    //finally block used to close resources
    try{
    if(stmt!=null)
    stmt.close();
    }catch(SQLException se2){
    }// nothing we can do
    try{
    if(conn!=null)
    conn.close();
    }catch(SQLException se){
    se.printStackTrace();
    }//end finally try
    }//end try
    System.out.println("Goodbye!");
    }//end main
    }//end FirstExample

将此代码添加到 Eclipse 中的新项目中。当我运行它时,我发生了这个错误

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at FirstExample.main(FirstExample.java:17)
Goodbye!

然后尝试了这个cmd命令

set classpath=C:\Program Files\MySQL\mysql-connector-java-3.1.14\mysql-   
connector-java-3.1.14\mysql-connector-java-3.1.14-bin.jar;

还是同样的错误该怎么办,请给出一些解释以及您有值(value)的解决方案谢谢

最佳答案

pom.xml 中添加以下依赖项

<!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

如果您没有使用 maven,则将此 jar com.mysql.jdbc_5.1.5.jar 添加到您的 eclipse 构建路径右键单击项目-->构建路径-->配置构建路径-->库-->添加外部jar

关于java - 运行jdbc时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37752746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com