我在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();
}
你約一個名爲 「test_tbl1」 表描述了會談。但是你的代碼插入名爲「test_tbl」的表中。檢查你的桌子。 –
謝謝。我得到了我的輸出。 – Sakthivel