-1

我计划为我的应用程序创建一个数据库,该数据库自动从文本字段中获取名称。(在运行文本字段时不为空)。但我有语法错误。这个相同的代码在同一个项目的另一个 java 页面中运行。下面的代码接受创建任务。

 public void FacltyNameTable(){

      String fcltyName = fcltyUserNameTxt.getText();

              try{

            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection(DB_URL_table, USER, PASS);
            stmt = (Statement) conn.createStatement();

            String sql1 = "CREATE TABLE IF NOT EXISTS '"+fcltyName+"' " +
                    "(id INTEGER not NULL AUTO_INCREMENT, " +
                    " rollNo VARCHAR(255), " + 
                    " studentName VARCHAR(255), " + 
                    "CommunicationOral INTEGER, "+
                    "Communicationwritten INTEGER, "+
                    "Leadership INTEGER, "+
                    "AnalyticalAbilities INTEGER, "+
                    "Interpersonalskills INTEGER, "+
                    "DecisionMakingSkills INTEGER, "+
                    "SelfConfidence INTEGER, "+
                    "Creativity INTEGER, "+
                    "Punctualityregularity INTEGER, "+
                    "GeneralAwareness INTEGER, "+
                    "Commitment INTEGER, "+
                    "Hardwork INTEGER, "+

                    " PRIMARY KEY ( id ))"; 

            stmt.executeUpdate(sql1);


             String insert1="INSERT IGNORE INTO '"+fcltyName+"'(id,rollNo,studentName,CommunicationOral,Communicationwritten,Leadership,AnalyticalAbilities,Interpersonalskills,DecisionMakingSkills,SelfConfidence,Creativity,Punctualityregularity,GeneralAwareness,Commitment,HardWork)VALUES(1,'1','ABHIJITH V GEORGE ',0,0,0,0,0,0,0,0,0,0,0,0)";
            stmt.executeUpdate(insert1);

 }catch(SQLException se){
            //Handle errors for JDBC
            se.printStackTrace();
    }catch(Exception e){
    //Handle errors for Class.forName
    e.printStackTrace();
    }finally{
        //finally block used to close resources
  try{
     if(stmt!=null)
        conn.close();
  }catch(SQLException se){
  }// do nothing
  try{
     if(conn!=null)
        conn.close();
  }catch(SQLException se){
     se.printStackTrace();
  }//end finally try
 }//end try
 }

以下显示错误

 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 ''' (id INTEGER not NULL AUTO_INCREMENT,  rollNo VARCHAR(255),  studentName VARCH' at line 1

当我将 'String sql1 = "CREATE TABLE IF NOT EXISTS"+fcltyName+"' " +........更改String sql1 = "CREATE TABLE IF NOT EXISTS fcltyName " +.......为完美工作时。我认为我的查询是错误的。我需要帮助

4

3 回答 3

2

如下改变这条线有帮助吗?(删除单引号)

        String sql1 = "CREATE TABLE IF NOT EXISTS "+fcltyName+" " +
于 2013-08-12T16:42:11.370 回答
1

fcltyUserNameTxt的可能是空的。您可以尝试记录:

String fcltyName = fcltyUserNameTxt.getText();
System.out.println(fcltyName); // ??
于 2013-08-12T16:59:03.530 回答
0

我使用 Connection conn=Drivermanager....等不使用类型转换。也总是使用准备好的语句。它整洁而安全。

一个小错误。例如。id int(10) not null 必须使用 auto_increment

必须给出 int 的大小。假设 mysql

于 2013-08-12T16:35:18.657 回答