0

鉴于此代码:

package db;
import java.io.*;
import java.sql.*;


public class Connect 
{


    // creating a table for each type person in the bank 

    public void createTable(Statement state,String tableType) throws SQLException
    {
        state.executeUpdate (

                "CREATE TABLE IF NOT EXISTS "+ tableType +" ("
                + "FirstName CHAR(20), LastName CHAR(20),"
                + "Address CHAR(50), PhoneNumber CHAR(20),"
                + "UserName CHAR(20), Password CHAR(20))");
    }



    public void insertDataToTable(Statement statement , String table 
            ,String firstName,String lastName,String address, String phoneNumber , String userName, String password)
    {
        try
        {
            statement.executeUpdate("INSERT INTO table (`FirstName` ,`LastName` , `Address` , `PhoneNumber` , `UserName` , `Password`) " +
                    "values ( '"+firstName+"','"+lastName+ "','"+address+"','"+phoneNumber+"','"+userName+ "'," +password+")");



        }

        catch(Exception e)
        {
            System.out.println(e.toString());
        }

    }


    public void start()
    {

        System.out.println("Database creation example!");
        Connection con = null;
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost","root","root");
            try
            {

                Statement st = con.createStatement();

                // create a database 

                st.executeUpdate("CREATE DATABASE IF NOT EXISTS Personnel"); 
                st.executeUpdate("USE Personnel");

                // create tables

                //Create a table for each user type!
                createTable(st, "ClientsTable");
                createTable(st, "ClerksTable");
                createTable(st, "ManagersTable");
                createTable(st, "AdminsTable");

                this.insertDataToTable(st, "ClientsTable", "my", "name", "is", "erl", "I", "think");

                ResultSet rs = st.executeQuery("SELECT `FirstName` FROM `ClientsTable`");
                while (rs.next() == true)
                { 
                    System.out.println(rs.getString("FirstName")); 
                }



            } // end try  

            catch (SQLException s)
            {
                System.out.println("SQL statement is not executed!");
            }


        } // end try 



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


    }  // end start



}

当我从我的 Main 执行时:

package db;

public class Main {

    public static void main(String [ ] args)
    {

        Connect myConnection = new Connect();

        myConnection.start();
    }

}

并在方法中达到该行start()

this.insertDataToTable(st, "ClientsTable", "my", "name", "is", "erl", "I", "think");

我从服务器获得以下输出(使用 try/catch):

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table (`FirstName` ,`LastName` , `Address` , `PhoneNumber` , `UserName` , `Passw' at line 1

我查了手册,但似乎找不到问题的根源。知道这里有什么问题吗?谢谢

4

4 回答 4

1

我相信你的表名是一个变量。但是,您直接使用了字符串“表”。但这不是问题。

您的password变量值需要单引号。

"values ( '"+firstName+"','"+lastName+ "','"+address+"','"+phoneNumber+"','"+userName+ "','" +password+"')");
于 2012-08-04T06:20:39.007 回答
1

您在列名周围使用波浪号“`”而不是引号“'”。

由于您没有任何分隔符,您可以从列名中删除所有波浪号/引号,或者将它们全部替换为引号“'”

于 2012-08-04T06:21:11.440 回答
1

请注意,语句中缺少您的表名。

statement.executeUpdate("INSERT INTO table (

改用以下

statement.executeUpdate("INSERT INTO `" + table + "` (
于 2012-08-04T06:23:32.717 回答
0

在您的价值观中,密码未包含在引号内。密码是字符串,但您的代码将其视为数字。

于 2012-08-04T11:34:19.957 回答