JDBC连接Mysql

错误提示1:Access denied for user 'root'@'localhost' (using password: YES)

解决方法: 这是由于Mysql的密码错误引起的,请确认密码正确!

错误提示2:Column count doesn't match value count at row 1

解决方法: 数据表的结构和你的SQL语句的结构不同。

顺带附带一段JDBC连接Mysql的代码:

import java.sql.*;

public class Main
{
    public static void main(String[] args)
    {

        String user = root;
        String password = liqiang6;
        String url = jdbc:mysql://localhost:3306/jsforum;
        String driver = com.mysql.jdbc.Driver;
        // String driver = org.gjt.mm.mysql.Driver;
        String tableName = forum_users;
        String sqlstr;
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try
        {
            Class.forName(driver);
            con = DriverManager.getConnection(url, user, password);
            stmt = con.createStatement();

            sqlstr = INSERT INTO 
                    + forum_users(user_name,password,email,registerdate,type, avatar, member_title, signature) 
                    + VALUES( + admin + ,password( + admin
                    + ), + [email protected]
                    + ,2010-9-11,user, 12,12,12);
            stmt.executeUpdate(sqlstr);

            sqlstr = select * from  + tableName;
            rs = stmt.executeQuery(sqlstr);

            ResultSetMetaData rsmd = rs.getMetaData();
            int j = 0;
            j = rsmd.getColumnCount();
            for (int k = 0; k < j; k++)
            {
                System.out.print(rsmd.getCatalogName(k + 1));
                System.out.print(t);
            }
            System.out.println();
            while (rs.next())
            {
                for (int i = 0; i < j; i++)
                {
                    System.out.print(rs.getString(i + 1));
                    System.out.print(t);
                }
                System.out.println();
            }
        }
        catch (ClassNotFoundException e1)
        {
            System.out.println(数据库驱动不存在!);
            System.out.println(e1.toString());
        }
        catch (SQLException e2)
        {
            System.out.println(数据库存在异常!);
            System.out.println(e2.toString());
        }
        finally
        {
            try
            {
                if (rs != null)
                    rs.close();
                if (stmt != null)
                    stmt.close();
                if (con != null)
                    con.close();
            }
            catch (SQLException e)
            {
                System.out.println(e.toString());
            }
        }
    }
}