2014-02-23 39 views
-1

我在MySQL中有下表「test_tbl1」。它有列「id,名字,薩爾」。我在eclipse中使用以下代碼並試圖從Excel中插入數據。我從Excel中獲取的每一行都出現以下錯誤。我很驚訝這個錯誤(MySQLSyntaxErrorException:'字段列表'中的未知列'名稱')

FAILED: testDataProviderExample("101", "sam", "10000") 
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'name' in 'field list' 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
    at com.mysql.jdbc.Util.getInstance(Util.java:386) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) 

public class dbcon extends SeleneseTestBase{ 
Connection conn = null; 
Statement stmt = null; 
@BeforeTest 
public static void connection() 
{ 

     try { 

      Class.forName("com.mysql.jdbc.Driver"); 
      System.out.println("hi"); 

     } catch (ClassNotFoundException e1) { 

      // TODO Auto-generated catch block 

      e1.printStackTrace(); 

     } 

} 

@BeforeTest  

public void MysqlConnection() //we need to add the Dataprovider name[name="DP"] to pass the data from excel sheet 

{ 

    try { 

     Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "admin"); 

     Statement stmt = conn.createStatement(); 

     System.out.println("hi1"); 

    } catch (SQLException e) { 

     e.printStackTrace(); 

     return; 

    } 
       System.out.println("Testing Testfile1"); 

} 



@DataProvider(name = "DP1") 

public Object[][] createData1() throws Exception{ 

    Object[][] retObjArr=getTableArray("C:\\my folder\\Kenscio\\New folder\\data4.xls","DataPool", "mysqldata"); 

    return(retObjArr); 

} 



@Test (dataProvider = "DP1") 

public void testDataProviderExample(String empid, String name, String sal) throws Exception { 

    System.out.println("id: " + empid); 

    System.out.println("name: " + name); 

    System.out.println("sal: " + sal); 
    System.out.println("hi2"); 
    String sql = "insert into test_tbl(id,name,sal)" + "values(?,?,?)"; 
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "admin"); 
    PreparedStatement preparedStatement = conn.prepareStatement(sql); 
    preparedStatement.setString(1, "id"); 
    preparedStatement.setString(2, "name"); 
    preparedStatement.setString(3, "sal"); 
    preparedStatement.executeUpdate(); 
    } 
+1

你約一個名爲 「test_tbl1」 表描述了會談。但是你的代碼插入名爲「test_tbl」的表中。檢查你的桌子。 –

+0

謝謝。我得到了我的輸出。 – Sakthivel

回答

1

你說你有一個表名test_tbl1,但您的查詢表示

String sql = "insert into test_tbl(id,name,sal)" + "values(?,?,?)"; 

我認爲你必須修改SQL是這樣的:

String sql = "insert into test_tbl1 (id,name,sal)" + "values(?,?,?)"; 
1

錯誤說沒有柱子n在要插入的表中名爲name。這可能是因爲您有一張名爲test_tbl1的表格,但您的代碼指的是test_tbl。這假定test_tbl存在並且沒有該列。嘗試使用mysql客戶端手動運行SQL。

另外,您的表名很差,我假設它是一張員工表,如果是這樣稱呼員工,您將避免類似的錯誤。

下級聯是不必要的:

String sql = "insert into test_tbl(id,name,sal)" + "values(?,?,?)";

相關問題