日期:2014-05-16 浏览次数:20539 次
DB_Select_Demo.java
?
package com.syc.test.DB; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.syctest.bean.UpdateListBean; public class DB_Select_Demo { public static void main(String[] args) { System.out.println(selectDB()); } public static List<UpdateListBean> selectDB() { Connection conn = null; Statement st = null; ResultSet rs = null; List<UpdateListBean> list=new ArrayList<UpdateListBean>(); String text = "00"; // mysql的加载JDBC驱动程序 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("驱动加载错误"); e.printStackTrace(); } // 与mysql数据库建立连接 try { conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/sva_rec", "root", "mysql"); } catch (SQLException e) { System.out.println("数据库链接错误"); e.printStackTrace(); } // 创建Statement try { st = conn.createStatement(); // 创建一个JDBC声明 String query ="SELECT dt, year,month FROM date_drv ORDER BY dt,MONTH DESC LIMIT 1,10"; rs = st.executeQuery(query); while (rs.next()) { UpdateListBean ulBean=new UpdateListBean(); ulBean.setYear(rs.getString("year")); ulBean.setDt(rs.getString("dt")); ulBean.setMonth(rs.getString("month")); list.add(ulBean); } } catch (SQLException e1) { System.out.println("数据操作错误"); e1.printStackTrace(); } // 关闭数据库 try { if (rs != null) { rs.close(); rs = null; } if (st != null) { st.close(); st = null; } if (conn != null) { conn.close(); conn = null; } } catch (Exception e) { System.out.println("数据库关闭错误"); e.printStackTrace(); } return list; } }
?
?
UpdateList2Json.java
package com.syctest.JSON; import java.util.ArrayList; import java.util.List; import net.sf.json.JSONArray; import com.syc.test.DB.DB_Select_Demo; import com.syctest.bean.UpdateListBean; public class UpdateList2Json { public static void main(String[] args) { UpdateList2Json(); } private static void UpdateList2Json() { List<UpdateListBean> list = new ArrayList<UpdateListBean>(); list = DB_Select_Demo.selectDB(); for (int i = 0; i < list.size(); i++) { // System.out.println(list.get(i).getYear()+" "+list.get(i).getMonth()+" "+list.get(i).getDt() // ); } for (int i = 0; i < list.size(); i++) { int maxNum = Integer.parseInt(list.get(0).getMonth()); int objNum = Integer.parseInt(list.get(i).getMonth()) * 100 / maxNum; System.out.println(maxNum + " " + objNum); list.get(i).setRate(String.valueOf(objNum)); } for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).getYear() + " " + list.get(i).getMonth() + " " + list.get(i).getDt() + " " + list.get(i).getRate()); } JSONArray ja = JSONArray.fromObject(list); System.out.println(ja.toString()); } }
?
?
UpdateListBean.java
package com.syctest.bean; public class UpdateListBean { public String rate; public String getRate() { return rate; } public void setRate(String rate) { this.rate = rate; } public String year; public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getMonth() { return month; } public void setMonth(String month) { this.month = month; } public String getDt() { return dt; } public void setDt(String dt) { this.dt = dt; } public String month; public String dt; }
?