1. 程式人生 > >JDBC連線資料庫mysql;http介面引數取資料庫的值

JDBC連線資料庫mysql;http介面引數取資料庫的值

package mm.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBC {

 public static final String url = "jdbc:mysql://192.168.0.200:3306/test";
 public static final String name = "com.mysql.jdbc.Driver";
 public static final String user = "root";
 public static final String password = "root";

 public Connection conn = null;
 public PreparedStatement pst = null;

 public JDBC(String sql) {
  try {
   Class.forName(name);// 指定連線型別
   conn = DriverManager.getConnection(url, user, password);// 獲取連線
   pst = conn.prepareStatement(sql);// 準備執行語句
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public void close() {
  try {
   this.conn.close();
   this.pst.close();
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }

}

package mm.sql;

import java.sql.ResultSet;
import java.sql.SQLException;

import mm.utils.JDBC;

public class SqlData {
 static String sql = null;
 static JDBC db1 = null;
 static ResultSet ret = null;

 public static String getUserId(String userid) {
  sql = "select *from user_base where mobile='13612341234'";// SQL語句
  db1 = new JDBC(sql);// 建立DBHelper物件
  try {
   ret = db1.pst.executeQuery();// 執行語句,得到結果集
   while (ret.next()) {
    userid = ret.getString("userid");
    // String ufname = ret.getString("realname");
   } // 顯示資料
   ret.close();
   db1.close();// 關閉連線
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return userid;
 }

}

public class TestCase {
 static String sql = null;
 static JDBC db1 = null;
 static ResultSet ret = null;
 
 Map<String, String> parameters = new HashMap<String, String>();
 String userId = null;
 String apiVersion = "2.6";
 String devName = "iOS";
 String all = "QSFETTUGBVNEREWR;apiVersion:" +apiVersion + ";devName:" +devName + ";";
 String url = "http://192.168.0.200:8085/site-admin-v" + apiVersion;

 @Test(groups = { "pepo", "message" })
 public void getUserMessage() {
  
  parameters.put("apiVersion", apiVersion);
  parameters.put("devName", devName);
  parameters.put("userId", SqlData.getUserId(userId));

  String result = HttpUtils.sendGet(url + "/site/accuntState", parameters);
  System.out.println(result);
  Reporter.log(testCaseTitel);
  Reporter.log(result);
 }