日期:2019.11.14
博客期:116
星期四
基本的处理类
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.PreparedStatement; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import com.hive.basic.Bean; 11 12 public class SqlForBean { 13 public static String databasename = "hive";//数据库名称 14 public static String username = "root";//登录数据库的用户的用户名 15 public static String password = "nssxs2489615648";//登录数据库的用户的密码 16 //插入方法 17 public static void insert(Bean bean) 18 { 19 try { 20 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 21 Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/"+databasename, username, password); //链接本地MYSQL 22 String sql = "insert into result values ("; 23 sql += "'"+bean.getIp()+"',"; 24 sql += "'"+bean.getTime()+"',"; 25 sql += "'"+bean.getDay()+"',"; 26 sql += bean.getTraffic()+","; 27 sql += "'"+bean.getType()+"',"; 28 sql += "'"+bean.getId()+"')"; 29 30 PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(sql); 31 pstmt.executeUpdate(); 32 pstmt.close(); 33 34 conn.close(); 35 } catch (SQLException e) { 36 e.printStackTrace(); 37 } catch (Exception e) { 38 e.printStackTrace(); 39 } 40 } 41 public static void insert(List <Bean> list_bean) 42 { 43 int size = list_bean.size(); 44 for(int i=0;i<size;++i) 45 { 46 SqlForBean.insert(list_bean.get(i)); 47 } 48 } 49 public static List<Bean> getData(){ 50 List <Bean> list = new ArrayList<Bean>(); 51 52 try 53 { 54 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 55 Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/"+databasename, username, password); //链接本地MYSQL 56 String sql = " select * from result"; 57 Statement stmt = conn.createStatement(); 58 ResultSet rs = stmt.executeQuery(sql); 59 while(rs.next()) 60 { 61 String ip_s = rs.getString("ip"); 62 String time_s = rs.getString("time"); 63 String day_s = rs.getString("day"); 64 int traffic_s = rs.getInt("traffic"); 65 String type_s = rs.getString("type"); 66 String id_s = rs.getString("id"); 67 Bean bean = new Bean(ip_s,time_s,day_s,traffic_s,type_s,id_s); 68 list.add(bean); 69 } 70 }catch(ClassNotFoundException e){ 71 e.printStackTrace(); 72 }catch(IllegalAccessException e){ 73 e.printStackTrace(); 74 }catch(InstantiationException e){ 75 e.printStackTrace(); 76 }catch(SQLException e){ 77 e.printStackTrace(); 78 } 79 80 return list; 81 } 82 }