Q&A
###前提・実現したいこと
データベースから取得したデータをArrayListに格納したい
###発生している問題・エラーメッセージ
recというArrayListにデータべ―スから取得したproductidを格納しようとすると
MySQLDataExceptionが発生する以下の'4.562101972731E12'の部分を取得したいのですが、後ろから3つ目のE12の部分はデータベースに格納されているproductidにはついていない。本来は前から13桁の4562101972731の部分です。
'4.562101972731E12' in column '1' is outside valid range for the datatype INTEGER.
###該当のソースコード
java
1package analysis; 2 3import java.io.IOException; 4import java.sql.Connection; 5import java.sql.ResultSet; 6import java.sql.SQLException; 7import java.sql.Statement; 8import java.util.ArrayList; 9 10import javax.servlet.ServletException; 11import javax.servlet.annotation.WebServlet; 12import javax.servlet.http.HttpServlet; 13import javax.servlet.http.HttpServletRequest; 14import javax.servlet.http.HttpServletResponse; 15import javax.servlet.http.HttpSession; 16 17import connect.ConnectDatabase; 18 19/** 20 * Servlet implementation class AnalysisData 21 */ 22@WebServlet(asyncSupported = true, urlPatterns = { "/AnalysisData" }) 23public class AnalysisData extends HttpServlet { 24 private static final long serialVersionUID = 1L; 25 //PreparedStatement ps = null; 26 ResultSet rs = null; 27 Statement stmt = null; 28 29 /** 30 * @see HttpServlet#HttpServlet() 31 */ 32 public AnalysisData() { 33 super(); 34 } 35 36 /** 37 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 38 */ 39 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 40 HttpSession session = request.getSession(true); 41 42 //String campanyId = (String) session.getAttribute("campanyid"); 43 //int campanyid = Integer.parseInt(campanyId); 44 ArrayList<ArrayList<Integer>> saletbl = 45 new ArrayList<ArrayList<Integer>>(); 46 47 48 Connection con= ConnectDatabase.getConnection();//MySqlクラスのgetConnectionメソッドを使用 49 try { 50 String sql = "SELECT SALES.PRODUCTID,LIQUORS.LIQUORID,QUANTITY," 51 + "SEXID,LAYER_ID,WEATHER_ID,TEMPERATURE_ID FROM SALES,PRODUCTS,LIQUORS " 52 + "WHERE SALES.PRODUCTID = PRODUCTS.PRODUCTID AND " 53 + "PRODUCTS.LIQUORID = LIQUORS.LIQUORID AND SALES.CAMPANYID = 1 "; 54 //ps = con.prepareStatement(sql); 55 //ps.setInt(1, campanyid); 56 57 stmt = con.createStatement(); 58 // rs = ps.executeQuery(sql); 59 rs = stmt.executeQuery(sql); 60 61 while(rs.next()){//データベースにデータがあったら 62 ArrayList<Integer> rec = new ArrayList<Integer>(); 63 rec.add(rs.getInt("productid")); 64 rec.add(rs.getInt("liquorid")); 65 rec.add(rs.getInt("quantity")); 66 rec.add(rs.getInt("sexid")); 67 rec.add(rs.getInt("layer_id")); 68 rec.add(rs.getInt("weather_id")); 69 rec.add(rs.getInt("temperature_id")); 70 saletbl.add(rec); 71 } 72 if(saletbl!=null){//単価が取得できていれば 73 session.setAttribute("saletbl",saletbl); 74 } 75 } catch (SQLException e) { 76 e.printStackTrace(); 77 } finally{ 78 if(stmt != null) 79 { try{ stmt.close(); } catch(SQLException ignore){} } 80 if(rs != null) 81 { try{ rs.close(); } catch(SQLException ignore){} } 82 } 83 84 85 } 86 87} 88
###試したこと
java
1 while(rs.next()){//データベースにデータがあったら 2 ArrayList<Integer> rec = new ArrayList<Integer>(); 3 rec.add(rs.getInt("productid"));
の部分を
java
1 while(rs.next()){//データベースにデータがあったら 2 ArrayList<Integer> rec = new ArrayList<Integer>(); 3 rec.add((int)rs.getLong("productid"));
に変えるとExceptionは出ない代わりに全く違う数字が格納されてしまう。
(ちなみに格納されている数字は846704379)
###補足情報(言語/FW/ツール等のバージョンなど)
java EE
eclipse
windows 10
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2016/11/19 12:43
2016/11/19 12:48
2016/11/19 13:31