如何将mysql与java连接为html表单?

时间:2014-10-01 03:49:58

标签: java mysql jdbc

我有登录的html表单。我想通过JDBC连接使用Java将这些表单连接到MySQL,但是当我执行java文件时,它显示以下异常:

java.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.j ava:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
    at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at MissingRuzdi.MissingDbConnection.SqlConnection(Mis singDbConnection.java:28)
    at MissingRuzdi.MissingDbConnection.main(MissingDbCon nection.java:55)

我已经设置了connectorJ的类路径。

1 个答案:

答案 0 :(得分:0)

您好Avnish检查此编码

import java.sql.*;

public class getmySQLConnection 
{
        public static void main(String[] args) 
        {
                DB db = new DB();
                Connection conn=db.dbConnect(
                  "jdbc:mysql://localhost:3306/test", "root", "");
        }

}

class DB
{
        public DB() {}

        public Connection dbConnect(String db_connect_string,
          String db_userid, String db_password)
        {
                try
                {
                        Class.forName("com.mysql.jdbc.Driver").newInstance();
                        Connection conn = DriverManager.getConnection(
                          db_connect_string, db_userid, db_password);

                        System.out.println("connected");
                        return conn;

                }
                catch (Exception e)
                {
                        e.printStackTrace();
                        return null;
                }
        }
};