JDBC 中的异常使用 MySQL

Exception in JDBC using MySQL

我正在尝试在 java 中创建一个应用程序,它在 mysql 中有数据库。我在网上搜索了各种资源,并在属性->库->添加 JAR/Library.

中包含了 mysql-connector-java-5.0.8-bin.jar

但当我 运行 时它仍然显示 java.lang.ClassNotFound Exception

我正在使用 netbeans IDE 8.0 和 jdk 1.7.0_45 和 mysql server 5.6

这是我的 jdbc mysql 连接代码:

try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    System.out.println("Connecting to a selected database...");
    con = DriverManager.getConnection("jdbc:mysql://localhost/prj","root","root");
    System.out.println("Connected database successfully...");
    s=con.createStatement();
    String q="insert into call_log"+" values(now(),'room 1')";
    s.executeUpdate(q);
} catch(SQLException se) { 
    se.printStackTrace(); 
} catch(Exception e) { 
    System.out.println(e); 
} finally { 
    try { 
        if(con!=null) 
            con.close(); 
    } catch(SQLException se) { 
        se.printStackTrace(); 
    } 
}

此代码适用于 NetBeans 8.0.2

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DBConnect {

public static void main(String[] args) {
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        System.out.println("Connecting to a selected database...");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "user", "pass");
        System.out.println("Connected database successfully...");
        Statement s = con.createStatement();
        String q = "select * from table";
        s.execute(q);
    } catch (ClassNotFoundException | SQLException | InstantiationException | IllegalAccessException ex) {
        Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>mavenproject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
</dependencies>