使用Mysql-connector/j将Java连接到Mysql [英] connecting java to Mysql using Mysql-connector/j

查看:252
本文介绍了使用Mysql-connector/j将Java连接到Mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用netbeans 6.9 IDE及其功能将Java类连接到Mysql数据库. 如您所知,Mysql-connector驱动程序嵌入在netbeans中.我使用Mysql(connector/j)驱动程序建立了与Mysql数据库的新连接,一切正常,它显示了Mysql中的所有数据库以及这些数据库中的所有表,但是当我创建Java类来测试连接并开始操作时,当我调用Class.forName("com.mysql.jdbc.Driver").newInstance();时抛出ClassNotFoundException IE.有没有司机?为什么我有这个例外?当我连接到derby嵌入式数据库时会发生同样的事情吗?你能帮我吗? 这是我的测试课

I use netbeans 6.9 IDE and its features to connect my java class to Mysql database. as you know, Mysql-connector driver is embedded in netbeans. I make new connection to Mysql database using Mysql(connector/j) driver, and every thing is okay, it displays all databases in Mysql and all tables in these databases, but when I create my java class to test the connetion and start manipulation, ClassNotFoundException is thrown when I call Class.forName("com.mysql.jdbc.Driver").newInstance(); ie. there is no driver ? why i got that exception ? the same happened when I connect to derby embedded db? can you help? here my test class

public static void main(String[] args) {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            System.out.println("driver true");
            con = DriverManager.getConnection("jdbc:mysql:///test",
                    "root", "123456");
            if (!con.isClosed()) {
                System.out.println("Successfully connected to "
                        + "MySQL server using TCP/IP...");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Exception: " + e.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
            }
        }
    }

推荐答案

您必须将mysql连接器.jar文件(库+添加库+ Mysql JDBC驱动程序)添加到项目中.

You have to add the mysql connector .jar file (Libraries + Add Library + Mysql JDBC Driver) to your project.

这篇关于使用Mysql-connector/j将Java连接到Mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆