/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.norstc.ambleinfo.service;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.norstc.ambleinfo.entity.Stock;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author zhangjh
 */
@Named(value = "stockBean")
@SessionScoped
public class StockBean implements Serializable {

    
    public List<Stock> getStocks() throws ClassNotFoundException,SQLException{
        Connection connect = null;
        String url ="jdbc:mysql://localhost:3306/ambleinfo_db?useUnicode=true&amp;characterEncoding=UTF-8&useSSL=false";
        String username = "root";
        String password = "toor";
        System.out.println("connceted to mysql");
        try{
            Class.forName("com.mysql.jdbc.Driver");
            connect = (Connection) DriverManager.getConnection(url, username, password);
            
        }catch(SQLException ex){
            System.out.println("in exception");
            System.out.println(ex.getMessage());
        }
        
        List<Stock> stocks = new ArrayList<Stock>();
        PreparedStatement pstmt = (PreparedStatement) connect.prepareStatement("select * from ai_tb_stocks");
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()){
            Stock stock = new Stock();
            stock.setStockId(rs.getInt("stock_id"));
            stock.setStockCode(rs.getString("stock_code"));
            stock.setStockName(rs.getString("stock_name"));
            stock.setCurrentPrice(rs.getFloat("current_price"));
            stock.setAiPrice(rs.getFloat("ai_price"));
            stock.setAiRoi(rs.getFloat("ai_roi"));
            System.out.println("connceted to mysql33" + stock.getStockCode());
            stocks.add(stock);
        }
         System.out.println("connceted to mysql22");
        //close connection
        rs.close();
        pstmt.close();
        connect.close();
        
        return stocks;
    }
    /**
     * Creates a new instance of StockBean
     */
    public StockBean() {
    }
    
}

@@ -593,16 +593,83 @@
 l.List;%0D
+%0Aimport java.util.logging.Level;%0D%0Aimport java.util.logging.Logger;%0D
 %0A%0D%0A/**%0D%0A
@@ -794,16 +794,49 @@
 le %7B%0D%0A%0D%0A
+    private List%3CStock%3E stocks;%0D%0A
     %0D%0A  
@@ -871,88 +871,309 @@
 ks()
- throws ClassNotFoundException,SQLException%7B%0D%0A        Connection connect = null;
+%7B%0D%0A        %0D%0A        return stocks;%0D%0A    %7D%0D%0A    /**%0D%0A     * Creates a new instance of StockBean%0D%0A     */%0D%0A    public StockBean() %7B%0D%0A        stocks = new ArrayList%3CStock%3E();%0D%0A        %0D%0A         Connection connect = null;%0D%0A         PreparedStatement pstmt = null;%0D%0A         ResultSet rs=null;%0D%0A         
 %0D%0A  
@@ -1427,16 +1427,39 @@
   try%7B%0D%0A
+            try %7B%0D%0A    
         
@@ -1519,118 +1519,30 @@
     
-connect = (Connection) DriverManager.getConnection(url, username, password);%0D%0A            %0D%0A        %7Dcatch(SQL
+%7D catch (ClassNotFound
 Exce
@@ -1550,16 +1550,17 @@
 tion ex)
+ 
 %7B%0D%0A     
@@ -1570,95 +1570,90 @@
     
+  
-System.out.println(%22in exception%22);%0D%0A            System.out.println(ex.getMessage());%0D%0A
+  Logger.getLogger(StockBean.class.getName()).log(Level.SEVERE, null, ex);%0D%0A    
     
@@ -1671,89 +1671,102 @@
     
-%0D%0A    
     
-List%3CStock%3E stocks = new ArrayList%3CStock%3E();%0D%0A        PreparedStatement
+connect = (Connection) DriverManager.getConnection(url, username, password);%0D%0A            
  pst
@@ -1855,25 +1855,19 @@
         
-ResultSet
+   
  rs = ps
@@ -1883,24 +1883,25 @@
 teQuery();%0D%0A
+ 
         whil
@@ -2441,54 +2441,189 @@
     
- System.out.println(%22connceted to mysql22%22);%0D%0A
+%0D%0A        %7Dcatch(SQLException ex)%7B%0D%0A            System.out.println(%22in exception%22);%0D%0A            System.out.println(ex.getMessage());%0D%0A        %7Dfinally%7B%0D%0A            try %7B%0D%0A        
     
@@ -2654,16 +2654,37 @@
         
+       
+ if(rs!=null) 
 rs.close
@@ -2692,24 +2692,48 @@
 );%0D%0A        
+   
+     if(pstmt!=null) 
 pstmt.close(
@@ -2740,24 +2740,50 @@
 );%0D%0A        
+    
+    if(connect!=null) 
 connect.clos
@@ -2800,127 +2800,169 @@
     
-%0D%0A
     
-    return stocks;
+%7D catch (SQLException ex) %7B
 %0D%0A    
-%7D%0D%0A
     
-/**%0D%0A     * Creates a new instance of StockBean%0D%0A     */%0D%0A    public StockBean() %7B
+        Logger.getLogger(StockBean.class.getName()).log(Level.SEVERE, null, ex);%0D%0A            %7D%0D%0A        %7D%0D%0A  %0D%0A        
 %0D%0A  

修改StockBean，使其更合规范