我正在使用 Eclipse Juno、GWT、Java。我正在尝试将照片上传到 MySQL 数据库(我使用 GWTUpload SingleUpload 获取图像)。在我之前的帖子(如何从 GWTUpload SingleUploader 将照片保存到 MySQL?)中,我试图在上传之前将图像转换为字节。然后我被告知我可以直接上传。因此,我更改了代码并遵循 GWT 建议,最终将变量定义为“PreloadedImage photo = new PreloadedImage();”。但是,该页面不再加载,并且出现以下错误。
[删除错误和视图以允许添加更多代码]
相关的服务器端代码:
public YthMmbrSectDtls createYouthMember(String youthMemberId,
String surname, String firstname, java.sql.Date dob,
PreloadedImage photograph, java.sql.Date archived, String sectionDetailsId,
String section, String pack, java.sql.Date startDate,
java.sql.Date endDate) {
YthMmbrSectDtls ythMmbrSectDtls = null; // necessary unless you do something in the exception handler
ResultSet result = null;
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(
"INSERT INTO at_cub_details at_section_details" +
" (cd_surname, cd_first_name, cd_dob, cd_photograph, cd_archived," +
" sd_section, sd_pack, sd_start_date, sd_end_date) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
ps.setString(1, surname);
ps.setString(2, firstname);
ps.setBlob(3, (java.sql.Blob) photograph);
ps.setDate(4, (java.sql.Date) dob);
ps.setDate(5, (java.sql.Date) archived);
ps.setString(6, section);
ps.setString(7, pack);
ps.setDate(8, (java.sql.Date) startDate);
ps.setDate(9, (java.sql.Date) endDate);
ps.executeUpdate();
}
catch (SQLException e) {
//do stuff on fail
System.out.println("SQLException createYouthMember 1.");
e.printStackTrace();
user = null;
}
finally {
if (result != null) {
try {
result.close();
}
catch (SQLException e) {
System.out.println("SQLException createYouthMember 2.");
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
}
catch (SQLException e) {
System.out.println("SQLException createYouthMember 3.");
e.printStackTrace();
}
}
}
return ythMmbrSectDtls;
}
DBConnection 类
package org.AwardTracker.server;
import gwtupload.client.PreloadedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.server.Base64Utils;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import org.AwardTracker.client.BCrypt;
import org.AwardTracker.client.Base64Decode;
import org.AwardTracker.client.DBConnection;
import org.AwardTracker.client.SectionDetails;
import org.AwardTracker.client.User;
import org.AwardTracker.client.YouthMember;
import org.AwardTracker.client.YthMmbrSectDtls;
import org.AwardTracker.server.Base64Encode;
import org.AwardTracker.server.Base64Encode2;
public class MySQLConnection extends RemoteServiceServlet implements DBConnection {
private Connection conn = null;
private String status;
private String url = "jdbc:mysql://localhost:3306/awardtracker";
private String user = "ss";
private String pass = "ss";
public MySQLConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, pass);
} catch (Exception e) {
//NEVER catch exceptions like this
System.out.println("Error connecting to database - not good eh");
e.printStackTrace();
}
}
public User authenticateUser(String userName, String pass, String level1, java.sql.Date archived1) {
User user = null; // necessary unless you do something in the exception handler
ResultSet result = null;
PreparedStatement ps = null;
String stored_hash = null;
try {
ps = conn.prepareStatement(
"select * from at_accounts where acc_email_address = \"" + userName + "\"");
result = ps.executeQuery();
while (result.next()) {
user = new User(result.getString(1), result.getString(2), result.getString(3), null);
stored_hash = result.getString(3);
}
}
catch (SQLException e) {
//do stuff on fail
System.out.println("SQLException authenticateUser 1.");
e.printStackTrace();
}
finally {
if (result != null) {
try {
result.close();
}
catch (SQLException e) {
System.out.println("SQLException authenticateUser 2.");
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
}
catch (SQLException e) {
System.out.println("SQLException authenticateUser 3.");
e.printStackTrace();
}
}
}
if (BCrypt.checkpw(pass, stored_hash)) {
} else {
user = null;
}
return user;
}
public User duplicateUser(String userName, String pass, String level1, java.sql.Date archived1) {
User user = null; // necessary unless you do something in the exception handler
ResultSet result = null;
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(
"select * from at_accounts where acc_email_address = \"" + userName + "\"");
result = ps.executeQuery();
while (result.next()) {
user = new User(result.getString(1), null, null, null);
}
}
catch (SQLException e) {
//do stuff on fail
System.out.println("SQLException duplicateUser 1.");
e.printStackTrace();
}
finally {
if (result != null) {
try {
result.close();
}
catch (SQLException e) {
System.out.println("SQLException duplicateUser 2.");
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
}
catch (SQLException e) {
System.out.println("SQLException duplicateUser 3.");
e.printStackTrace();
}
}
}
return user;
}
public User createUser(String userName, String pass, String level1, java.sql.Date archived1) {
User user = null; // necessary unless you do something in the exception handler
ResultSet result = null;
PreparedStatement ps = null;
String pw_hash = BCrypt.hashpw(pass, BCrypt.gensalt());
try {
ps = conn.prepareStatement(
"INSERT INTO at_accounts (acc_email_address, acc_password) " +
"VALUES (?, ?)");
ps.setString(1, userName);
ps.setString(2, pw_hash);
ps.executeUpdate();
}
catch (SQLException e) {
//do stuff on fail
System.out.println("SQLException createUser 1.");
e.printStackTrace();
user = null;
}
finally {
if (result != null) {
try {
result.close();
}
catch (SQLException e) {
System.out.println("SQLException createUser 2.");
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
}
catch (SQLException e) {
System.out.println("SQLException createUser 3.");
e.printStackTrace();
}
}
}
return user;
}
public List<YouthMember> getYM(String id, String surname, String first_name, java.sql.Date dob, String photograph, java.sql.Date archived, String pack) {
List<YouthMember> youthMemberList = new ArrayList<YouthMember>();
//YouthMember youthMember = null; // necessary unless you do something in the exception handler
ResultSet result = null;
PreparedStatement ps = null;
String imageString = null;
try {
ps = conn.prepareStatement(
"SELECT at_cub_details.*" +
" FROM at_cub_details, at_section_details" +
" WHERE (at_cub_details.cd_id = at_section_details.cd_id" +
" AND at_section_details.sd_pack = \"" + pack + "\"" + ")");
result = ps.executeQuery();
while (result.next()) {
imageString = getImageData(result.getString(1));
YouthMember youthMember = new YouthMember(result.getString(1), result.getString(2), result.getString(3), result.getDate(4), imageString, result.getDate(6));
youthMemberList.add(youthMember);
}
}
catch (SQLException e) {
//do stuff on fail
System.out.println("SQLException getYouthMember 1.");
e.printStackTrace();
}
finally {
if (result != null) {
try {
result.close();
}
catch (SQLException e) {
System.out.println("SQLException getYouthMember 2.");
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
}
catch (SQLException e) {
System.out.println("SQLException getYouthMember 3.");
e.printStackTrace();
}
}
}
return youthMemberList;
}
public String getImageData(String id){
ResultSet result = null;
PreparedStatement ps = null;
String imageDataString = null;
String base64 = null;
try {
// Read in the image from the database.
ps = conn.prepareStatement(
"SELECT at_cub_details.cd_photograph " +
"FROM at_cub_details " +
"WHERE at_cub_details.cd_id = \"" + id + "\"");
result = ps.executeQuery();
while (result.next()) {
java.sql.Blob imageBlob = result.getBlob(1);
byte[] imageData = imageBlob.getBytes(1, (int) imageBlob.length());
//Convert Image byte array into Base64 String
imageDataString = encodeImage(imageData);
imageDataString = "data:image/jpeg;base64,"+imageDataString;
}
}
catch (SQLException e) {
//do stuff on fail
System.out.println("SQLException getImageData 1.");
e.printStackTrace();
user = null;
}
finally {
if (result != null) {
try {
result.close();
}
catch (SQLException e) {
System.out.println("SQLException getImageData 2.");
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
}
catch (SQLException e) {
System.out.println("SQLException getImageData 3.");
e.printStackTrace();
}
}
}
return imageDataString;
}
/**
* Encodes the byte array into base64 string
* @param imageByteArray - byte array
* @return String a {@link java.lang.String}
*/
public static String encodeImage(byte[] imageByteArray) {
return Base64Encode2.encode(imageByteArray);
}
public YthMmbrSectDtls createYouthMember(String youthMemberId,
String surname, String firstname, Date dob,
String password, Date archived, String sectionDetailsId,
String section, String pack, Date startDate, Date endDate) {
YthMmbrSectDtls ythMmbrSectDtls = null; // necessary unless you do something in the exception handler
ResultSet result = null;
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(
"INSERT INTO at_cub_details at_section_details" +
" (cd_surname, cd_first_name, cd_dob, cd_archived," +
" sd_section, sd_pack, sd_start_date, sd_end_date) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
ps.setString(1, surname);
ps.setString(2, firstname);
ps.setDate(3, (java.sql.Date) dob);
ps.setDate(4, (java.sql.Date) archived);
ps.setString(5, section);
ps.setString(6, pack);
ps.setDate(7, (java.sql.Date) startDate);
ps.setDate(8, (java.sql.Date) endDate);
ps.executeUpdate();
}
catch (SQLException e) {
//do stuff on fail
System.out.println("SQLException createYouthMember 1.");
e.printStackTrace();
user = null;
}
finally {
if (result != null) {
try {
result.close();
}
catch (SQLException e) {
System.out.println("SQLException createYouthMember 2.");
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
}
catch (SQLException e) {
System.out.println("SQLException createYouthMember 3.");
e.printStackTrace();
}
}
}
return ythMmbrSectDtls;
}
public YthMmbrSectDtls updateYouthMember(String id, String surname,
String firstname, java.sql.Date dob, java.sql.Date archived,
String section, String pack, java.sql.Date startDate,
java.sql.Date endDate) {
YthMmbrSectDtls ythMmbrSectDtls = null; // necessary unless you do something in the exception handler
ResultSet result = null;
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(
"UPDATE at_cub_details at_section_details" +
" (cd_surname, cd_first_name, cd_dob, cd_archived," +
" sd_section, sd_pack, sd_start_date, sd_end_date) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
ps.setString(1, surname);
ps.setString(2, firstname);
ps.setDate(3, (java.sql.Date) dob);
ps.setDate(4, (java.sql.Date) archived);
ps.setString(5, section);
ps.setString(6, pack);
ps.setDate(7, (java.sql.Date) startDate);
ps.setDate(8, (java.sql.Date) endDate);
ps.executeUpdate();
}
catch (SQLException e) {
//do stuff on fail
System.out.println("SQLException updateYouthMember 1.");
e.printStackTrace();
user = null;
}
finally {
if (result != null) {
try {
result.close();
}
catch (SQLException e) {
System.out.println("SQLException updateYouthMember 2.");
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
}
catch (SQLException e) {
System.out.println("SQLException updateYouthMember 3.");
e.printStackTrace();
}
}
}
return ythMmbrSectDtls;
}
public YouthMember readYM(String id, String surname, String first_name, java.sql.Date dob, String photograph, java.sql.Date archived) {
YouthMember youthMemberDetails = null; // necessary unless you do something in the exception handler
ResultSet result = null;
PreparedStatement ps = null;
String imageString = null;
System.out.println("ID received = " + id);
try {
ps = conn.prepareStatement(
"SELECT * " +
"FROM at_cub_details " +
"WHERE at_cub_details.cd_id = \"" + id + "\"");
// ps.getString(1, id);
// "SELECT * " +
// " FROM at_cub_details");
// " FROM at_cub_details " +
// " WHERE at_cub_details.cd_id = 2");
// " WHERE at_cub_details.cd_id = \"" + id + "\"");
result = ps.executeQuery();
while (result.next()) {
imageString = getImageData(result.getString(1));
youthMemberDetails = new YouthMember(result.getString(1), result.getString(2), result.getString(3), result.getDate(4), imageString, result.getDate(6));
}
}
catch (SQLException e) {
//do stuff on fail
System.out.println("SQLException readYM 1.");
e.printStackTrace();
}
finally {
if (result != null) {
try {
result.close();
}
catch (SQLException e) {
System.out.println("SQLException readYM 2.");
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
}
catch (SQLException e) {
System.out.println("SQLException readYM 3.");
e.printStackTrace();
}
}
}
return youthMemberDetails;
}
public SectionDetails invested(String id, String youth_member_id,
String section, String pack, java.sql.Date start_date,
java.sql.Date end_date) {
SectionDetails sectionDetails = null; // necessary unless you do something in the exception handler
ResultSet result = null;
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(
"SELECT MIN(start_date)" +
"FROM at_section_details" +
"WHERE cd_id = \"" + youth_member_id + "\"" +
"AND cd_pack = \"" + pack + "\"");
result = ps.executeQuery();
while (result.next()) {
sectionDetails = new SectionDetails(null, null, null, null, result.getDate(5), null);
}
}
catch (SQLException e) {
//do stuff on fail
System.out.println("SQLException invested 1.");
e.printStackTrace();
}
finally {
if (result != null) {
try {
result.close();
}
catch (SQLException e) {
System.out.println("SQLException invested 2.");
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
}
catch (SQLException e) {
System.out.println("SQLException invested 3.");
e.printStackTrace();
}
}
}
return sectionDetails;
}
}