我正在尝试使用Java servlet将文件数据从数据库传输到HTML。 文件数据是二进制格式。我正在将二进制格式数据转换为字符串格式,但我收到错误:
列索引超出范围:1,列数:0。
任何人都可以建议我哪里出错了吗?
这是我的Java代码:
package fileretrieve;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import dbConnection.Dbconn;
@MultipartConfig
public class FileRetrieve extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String onelevel = request.getParameter("onelevel");
String twolevel = request.getParameter("twolevel");
String threelevel = request.getParameter("threelevel");
String fourlevel = request.getParameter("fourlevel");
String fivelevel = request.getParameter("fivelevel");
String sevenlevel = request.getParameter("sevenlevel");
String eightlevel = request.getParameter("eightlevel");
Part filePart = request.getPart("data");
byte[] filecontent1 = new byte[] {78,73, 67,69,70,75,80,76,68};
String value = new String(filecontent1);
System.out.println(value);
Connection conn = null; // connection to the database
String message = null;
Statement st = null;// message will be sent back to client
try {
// connects to the database
conn = Dbconn.getConnection();
st = conn.createStatement();
// constructs SQL statement
String sql = "Select data from files1 where board=?,syllabus=?,class=?,chapters=?,state=?,terms=?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, onelevel);
statement.setString(2, twolevel);
statement.setString(3, threelevel);
statement.setString(4, fourlevel);
statement.setString(5, fivelevel);
statement.setBytes(6, filecontent1);
statement.setBytes(7, filecontent1);
statement.setBytes(8, filecontent1);
/* if (inputStream != null) {
// fetches input stream of the upload file for the blob column
statement.setBlob(6, inputStream);
}
*/
// statement.setBinaryStream(6, fis, (int)file.length());
// sends the statement to the database server
int row = statement.executeUpdate();
} catch (SQLException ex) {
message = "ERROR: " + ex.getMessage();
ex.printStackTrace();
} finally {
if (conn != null) {
// closes the database connection
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
// sets the message in request scope
request.setAttribute("Message", message);
// forwards to the message page
getServletContext().getRequestDispatcher("/home.html").forward(request, response);
}
}
}
数据库表:
CREATE TABLE files1
(
board text,
syllabus text,
class text,
subject text,
chapters text,
data bytea,
state text,
terms text
)
答案 0 :(得分:0)
您有8个参数,但查询只有6个(部分在哪里)