java connector mysql_使用Mysql-connector / j将java连接到Mysql

bd96500e110b49cbb3cd949968f18be7.png

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) {

}

}

}

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值