標籤:
在eclipse下編寫好的代碼:
1 import lrapi.lr; 2 import java.sql.Connection; 3 import java.sql.DriverManager; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 7 public class Actions { 8 //定義使用者名稱,密碼,串連的URL 9 String username = "root";10 String password = "123456";11 String url = "jdbc:mysql://127.0.0.1:3306/oa";12 String sql = "select * from itcast_user where id = 2";13 public int init() throws Throwable {14 //1註冊mysql驅動15 Class.forName("com.mysql.jdbc.Driver");16 return 0;17 }// end of init18 19 public int action() throws Throwable {20 //2串連mysql,匯入java.sql.Connection21 Connection conn = DriverManager.getConnection(url,username,password);22 //3擷取sql執行器的類,匯入java.sql.preparedstatement23 PreparedStatement ps = conn.prepareStatement(sql);24 //4執行sql語句,擷取到執行結果對象ResultSet(匯入)25 ResultSet set = ps.executeQuery();26 //列印擷取到的資料27 while(set.next()){28 String name = set.getString("name");29 System.out.println("name"+name);30 }31 //關閉掉ResultSet,PreparedStatement,Connection32 set.close();33 ps.close();34 conn.close();35 return 0;36 }// end of action37 38 public int end() throws Throwable {39 40 return 0;41 }// end of end42 43 /*44 //程式去執行的入口,在LR中不用寫45 public static void main(String[] args) throws Throwable{46 Actions actions = new Actions();47 actions.init();48 actions.action();49 actions.end();50 }*/51 }
拷貝到LR並檢查是否有語法錯誤。
設定LR回合設定:Run-time Settings
一切準備就緒,接下來看是否查詢到資料,如所示有資料返回證明指令碼跑通:
本文的指令碼只適用於調試用,因為還有需要最佳化的地方,比如添加參數化、事務、檢查點等。
LR介面測試---Java Vuser之jdbc調試前