数据库创建

时间:2013-08-12 16:32:01

标签: java database netbeans

我计划为我的应用程序创建一个数据库,它自动从文本字段中获取名称。(在运行文本字段中不为空)。但是我有语法错误。这个相同的代码在同一项目的另一个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 NOT EXIS "+fcltyName+"' " +........String sql1 = "CREATE TABLE IF NOT EXISTS fcltyName " +.......时,它完美无缺。我想我的查询是Wromg。我需要帮助

3 个答案:

答案 0 :(得分:2)

更改此行如下有帮助吗? (删除单引号)

        String sql1 = "CREATE TABLE IF NOT EXISTS "+fcltyName+" " +

答案 1 :(得分:1)

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

String fcltyName = fcltyUserNameTxt.getText();
System.out.println(fcltyName); // ??

答案 2 :(得分:0)

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

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

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