1

j2me の mysql データベース テーブルを取得する必要がある次のサーブレットを実行した後、java.io.eofexception が発生します。mysql データベースにテーブルを表示する j2me cldc アプリケーションを開発しています。j2me は、サーブレット jsp ページに表示される http エラーを表示し続けます。私が大学を卒業できるようにみんなを助けて!

パッケージ 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();
    }

}

}

4

0 に答える 0