简体   繁体   中英

Exception in JDBC using MySQL

I am trying to make an applicatio in java which has database in mysql. I have searched various sources on net and have included mysql-connector-java-5.0.8-bin.jar in properties->library->add JAR/Library.

But it still shows me java.lang.ClassNotFound Exception when i run it.

I am using netbeans IDE 8.0 and jdk 1.7.0_45 and mysql server 5.6

Here is my code for jdbc mysql connection:

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

This code is working in 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM