このコードを考えると:
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
マニュアルを確認しましたが、問題の原因がわかりません。ここで何が問題なのですか?ありがとう