试图将数据插入到MySQL表不工作(Java)的
问题描述:
鉴于此代码:试图将数据插入到MySQL表不工作(Java)的
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
}
当我从我的主要执行:
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
我检查了手册,但我似乎无法找到问题的根源。任何想法这里有什么问题?谢谢
答
你的表名是我相信的一个变量。但是你直接使用了字符串“table”。但这不是问题。
您的password
变量值需要单引号。
"values ('"+firstName+"','"+lastName+ "','"+address+"','"+phoneNumber+"','"+userName+ "','" +password+"')");
答
您在列名称周围使用代字号“`”而不是引号“'”。
因为你没有在其中任何分隔符,你可以从你的列名中删除所有波浪/报价,或只是引用替换所有的人““”
答
注意,你的表名是失踪该声明。以下代替
statement.executeUpdate("INSERT INTO `" + table + "` (
答
在你的价值观
statement.executeUpdate("INSERT INTO table (
使用,密码是不是一个引号括起来。 密码是一个字符串,但您的代码将其视为一个数字。
用你的实际表名替换'table'并在你的密码周围加上引号。 – 2012-08-04 06:16:23
什么是错误代码? – Kowser 2012-08-04 06:19:11
@juergend:我这样做:'的Statement.executeUpdate( “INSERT INTO ClientsTable('FirstName','LastName','Address','PhoneNumber','UserName','Password')” + \t \t \t \t \t “+”+“','”+'+'+'','“+'+''''',''+ + +'''',''+ lastName +'','”+地址+而不是我得到'com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:在'字段列表'中未知列'想' ' – ron 2012-08-04 06:20:28