我很震惊这个错误(MySQLSyntaxErrorException:'字段列表'中的未知列'名称')

时间:2014-02-23 12:35:15

标签: java mysql eclipse

我在MySQL中有下表“test_tbl1”。它有“id,name,sal”列。我在eclipse中使用以下代码并尝试从Excel中插入数据。我从excel获取的每一行都收到以下错误。

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();
    }

2 个答案:

答案 0 :(得分: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 :(得分:1)

错误表示您插入的表中没有名为name的列。这可能是因为您有一个名为test_tbl1的表,但您的代码引用了test_tbl。这假定存在test_tbl并且没有该列。尝试使用mysql客户端手动运行SQL。

另外,你的表名很差,我认为这是一张员工表,如果这样称之为员工,你就会避免类似的错误。

以下连接是不必要的:

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