插入数据时列名称错误

时间:2014-03-26 10:07:42

标签: jsp jdbc

我使用以下代码将数据插入Excel工作表。

<%@page import="java.sql.*"%>
<%
Connection con = null;
Statement stmnt = null;
try
{
    int updateQuery=0;
    String a=request.getParameter("comments");
    String b=request.getParameter("updatedl");
    String c=request.getParameter("sid");
    String d=request.getParameter("desc");
    String e=request.getParameter("pubcode");
    String f=request.getParameter("Type");
    String g=request.getParameter("status");
    String h=request.getParameter("DateRec");
    String i=request.getParameter("startD");
    String j=request.getParameter("AssignedD");
    String k=request.getParameter("sentToAE");
    String l=request.getParameter("RespFrmAE");
    String m=request.getParameter("VWRCmp");
    String n=request.getParameter("PS");
    String o=request.getParameter("TEst");
    String p=request.getParameter("Units");
    String q=request.getParameter("VWR");
    String r=request.getParameter("IE");
    String s=request.getParameter("RevDate");
    String t=request.getParameter("ReviewS");


Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
con = DriverManager.getConnection( "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=C:/jus/Excel1.xls; ReadOnly=False;");


stmnt = con.createStatement(); 
//String query="select column_name from 'Feb$'";
String query = "insert into [Feb$] ('Comments','No of lines updated in PSIS','Shipment ID',"+
"'SAP TXT Description', 'Phoenix Pub Code', 'Product Type', 'Status of the job', 'Date Request received',"+
"'Start Date', 'Date assigned', 'Date file sent to AE', 'Date response from AE',"+
"'Date completed --VWR', 'Assigned to PS', 'Time Estimate', 'Units', 'Date file needs to be sent to Vendor (VWR)',"+
"'Internal Errors', 'Review Date',  'Review Sent (Yes/No)') values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?S)";
PreparedStatement ps=con.prepareStatement(query);
ps.setString(1, a);
ps.setString(2, b);
ps.setString(3, c);
ps.setString(4, d);
ps.setString(5, e);
ps.setString(6, f);
ps.setString(7, g);
ps.setString(8, h);
ps.setString(9, i);
ps.setString(10, j);
ps.setString(11, k);
ps.setString(12, l);
ps.setString(13, m);
ps.setString(14, n);
ps.setString(15, o);
ps.setString(16, p);
ps.setString(17, q);
ps.setString(18, r);
ps.setString(19, s);
ps.setString(20, t);

//out.print(query);
ResultSet rs = ps.executeQuery();

if (updateQuery != 0) { %>
<br>
<TABLE style="background-color: #E3E4FA;" 
WIDTH="30%" border="1">
   <tr><th>Data is inserted successfully 
 in database.</th></tr>
</table>
<%
}

}
catch( Exception e )
{
out.println( e );
}
finally
{
try
{
stmnt.close();
con.close();
}
catch( Exception e )
{
out.println( e );
}
}

        %>

我收到以下错误。

java.sql.SQLException: [Microsoft][ODBC Excel Driver] The INSERT INTO statement contains the following unknown field name: ''No of lines updated in PSIS''. Make sure you have typed the name correctly, and try the operation again. 

我的Excel工作表是以下列名。

Comments
No of lines updated in PSIS
Shipment ID
SAP TXT Description
Phoenix Pub Code
Product Type
Status of the job
Date Request received
Start Date
Date assigned
Date file sent to AE
Date response from AE
Date completed --VWR
Assigned to PS
Time Estimate
Units
Date file needs to be sent to Vendor (VWR)
Internal Errors
Review Date
Review Sent (Yes/No)

由于

1 个答案:

答案 0 :(得分:0)

我认为你的代码在查询构建的插入部分有一些问题。

1首先是什么[2月$]? 2.你的最后一个参数是我认为误输入类似“?s”它应该只是“?,?,?”