我在运行以下servlet后得到一个java.io.eofexception,该servlet应该为我的j2me检索一个mysql数据库表。我正在开发一个j2me cldc应用程序,它应该在mysql数据库中显示一个表。现在j2me继续显示servlet jsp页面上显示的http错误。帮帮我们,所以我可以从大学毕业!
package midletservlet;
enter code here
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class getConn extends HttpServlet {
String query;
String printrow;
String id;
String car_model;
String price;
Statement stmt;
ResultSet res;
Connection conn;
@Override
public void init(){
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DataInputStream in = new DataInputStream((InputStream)request.getInputStream());
String db = in.readUTF();
String user = in.readUTF();
String pwd = in.readUTF();
String message = "jdbc:mysql://localhost:3306/"+db+","+user+","+pwd;
String mess = "no data";
try{
connect(db.toLowerCase().trim(), user.toLowerCase().trim(), pwd.toLowerCase().trim());
message +="100 ok";
} catch(Throwable t){
message +="200" +t.toString();
}
try{
String query = "SELECT id, car_model, price FROM car_prices";
stmt = conn.createStatement();
res = stmt.executeQuery(query);
}
catch(SQLException error){
}
boolean Records;
try {
Records = res.next();
if(!Records){
System.out.println(mess);
}
} catch (SQLException ex) {
Logger.getLogger(getConn.class.getName()).log(Level.SEVERE, null, ex);
}
try{
do{
id = res.getString(1);
car_model = res.getString(2);
price = res.getString(3);
printrow = id + " " + car_model + " " + price;
System.out.print(printrow);
}
while(res.next());
stmt.close();
}
catch(SQLException error){
System.err.print("data display error" +error);
}
response.setContentType("text/plain");
response.setContentLength(message.length());
PrintWriter out = response.getWriter();
out.println(message);
in.close();
out.close();
out.flush();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
private String connect(String db, String user, String pwd) throws Exception{
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306 /"+db,user,pwd);
Statement stmt = conn.createStatement();
ResultSet res = stmt.executeQuery("SELECT * from car_prices");
if(res.next()) {
return res.getString(1);
}
else {
return null;
}
} catch(Exception e){
return e.toString();
}
}
}