×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

请教大虾,一个简单的java 链接mysql数据库的程序,怎么一执行 Class.forName ("com.mysql.jdbc.Driver").newInstance (); 就出错了呢?但我在NetBean6.5的service里面直接操作数据库都没问题的。程序见内。什么地方的设置有问题么?用的mysql 5.1

本文发表在 rolia.net 枫下论坛package connect;
import java.sql.*;

public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Connection conn = null;

try
{
String userName = "root";
String password = "";
String url = "jdbc:mysql://localhost/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}

}更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / 请教大虾,一个简单的java 链接mysql数据库的程序,怎么一执行 Class.forName ("com.mysql.jdbc.Driver").newInstance (); 就出错了呢?但我在NetBean6.5的service里面直接操作数据库都没问题的。程序见内。什么地方的设置有问题么?用的mysql 5.1
    本文发表在 rolia.net 枫下论坛package connect;
    import java.sql.*;

    public class Main {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    Connection conn = null;

    try
    {
    String userName = "root";
    String password = "";
    String url = "jdbc:mysql://localhost/test";
    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection (url, userName, password);
    System.out.println ("Database connection established");
    }
    catch (Exception e)
    {
    System.err.println ("Cannot connect to database server");
    }
    finally
    {
    if (conn != null)
    {
    try
    {
    conn.close ();
    System.out.println ("Database connection terminated");
    }
    catch (Exception e) { /* ignore close errors */ }
    }
    }
    }

    }更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • dirver是否在运行环境的classpath 里? 另外, 别把Exception吃了,打出来看看
      • 在windows里的classpath里面加了c:\...\mysql-connector-java-5.1.6-bin.jar. 还是一样呢。
      • 用了e.printStackTrace()给出的结果,好像是说没有找到这个类com.mysql.jdbc.driver
        ava.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:169)
        at connect.Main.main(Main.java:25)
        • Have you setup the project library in your IDE? You need to add MySql connector jar file in project property window -> library category. Just a thought.
          • thanks a lot. IDE的库里面还有好几个分类,compile, run, compile test, run test, 把mysql connector jar file加到compile里面就好了。不过还是不大明白,compile的时候要连接这个库么?怎么没报错呢
            • you need to add .jar into library pool if you want to build a .war in dist and ship them with production.