Guest User

Untitled

a guest
Mar 8th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. package qc.autofun.java.extfun.sql;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import org.testng.annotations.DataProvider;
  8. import qc.autofun.java.web.base.TableInfo;
  9.  
  10. public class OperDb {
  11.  
  12.     List<TableInfo> tableInfoList = new ArrayList<TableInfo>();
  13.  
  14.     //链接数据库
  15.     public static Connection getConnection() throws SQLException, java.lang.ClassNotFoundException {
  16.         String dburl = "jdbc:mysql://127.0.0.1:3306/xxh";
  17.         Class.forName("com.mysql.jdbc.Driver");
  18.         String userName = "root";
  19.         String password = "root";
  20.         Connection con = DriverManager.getConnection(dburl, userName, password);
  21.         return con;
  22.     }
  23.  
  24.     //读取数据库并存放到list中
  25.     @DataProvider(name = "userpwd")
  26.     public List<TableInfo> readData() {
  27.         try {
  28.             Connection con = getConnection();
  29.             Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
  30.             ResultSet rs = stmt.executeQuery("select * from userpwd");
  31.  
  32.             while (rs.next()) {
  33.                 TableInfo ti = new TableInfo();
  34.                 ti.setCaseId(rs.getInt("caseID"));
  35.                 ti.setAccount(rs.getString("uName"));
  36.                 ti.setPassword(rs.getString("uPwd"));
  37.                 ti.setExpDialog(rs.getString("expDialog"));
  38.                 tableInfoList.add(ti);
  39.             }
  40.             stmt.close();
  41.             con.close();
  42.         } catch (java.lang.ClassNotFoundException e) {
  43.             System.err.println("ClassNotFoundException:" + e.getMessage());
  44.         } catch (SQLException ex) {
  45.             System.err.println("SQLException:" + ex.getMessage());
  46.         }
  47.         return tableInfoList;
  48.     }
  49.  
  50.     //根据VerifyResult来写数据库
  51.     public void updateData(String tag, int caseId) {
  52.         try {
  53.             Connection con = getConnection();
  54.             Statement stmt = con.createStatement();
  55.             ResultSet rs = stmt.executeQuery("select * from userpwd where caseID = " + caseId);
  56.             rs.first();
  57.             String s = rs.getString("expDialog");
  58.             stmt.executeUpdate("update userpwd set actDialog = '" + tag + "' where caseID = " + caseId);
  59.  
  60.             if (s.equals(tag)) {
  61.                 System.out.println("exptag:"+s);
  62.                 stmt = con.createStatement();
  63.                 stmt.executeUpdate("update userpwd set result = 'Pass' where caseID = " + caseId);
  64.             } else {
  65.                 System.out.println("exptag:"+s);
  66.                 stmt = con.createStatement();
  67.                 stmt.executeUpdate("update userpwd set result = 'Fail' where caseID = " + caseId);
  68.             }
  69.             rs.close();
  70.             stmt.close();
  71.             con.close();
  72.         } catch (java.lang.ClassNotFoundException e) {
  73.             System.err.println("ClassNotFoundException:" + e.getMessage());
  74.         } catch (SQLException ex) {
  75.             System.err.println("SQLException:" + ex.getMessage());
  76.         }
  77.  
  78.     }
  79.  
  80. }
Add Comment
Please, Sign In to add comment