標籤:
1 import lrapi.lr; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 8 public class Actions { 9 //定義使用者名稱,密碼,串連的URL10 String username = "root";11 String password = "123456";12 String url = "jdbc:mysql://127.0.0.1:3306/hk";13 //String sql = "select * from teach where id = ?";14 String sql = "update teach set name = ? where id = 2";15 Connection conn = null;16 PreparedStatement ps;17 ResultSet set = null;18 //所有的並發只執行一次的操作,放在static代碼塊裡19 static{20 21 //1註冊mysql驅動22 try{23 Class.forName("com.mysql.jdbc.Driver");24 }catch(ClassNotFoundException e){25 e.printStackTrace();26 }27 }28 public int init() throws Throwable {29 30 //2串連mysql,匯入java.sql.Connection31 conn = DriverManager.getConnection(url,username,password);32 //3擷取sql執行器的類,匯入java.sql.preparedstatement33 ps = conn.prepareStatement(sql);34 return 0;35 }// end of init36 37 public int action() throws Throwable {38 39 //字串轉換成整形40 //Integer.parseInt("<id>");41 //對sql語句中的?做賦值的操作,為了做參數化42 //ps.setInt(1, Integer.parseInt("<id>"));43 ps.setInt(1,123456);44 45 46 lr.start_transaction("shiwu");47 48 49 //4執行sql語句,擷取到執行結果對象ResultSet(匯入)50 //set = ps.executeQuery();51 52 //如果是insert,update,delete53 int rows = ps.executeUpdate();54 if(rows > 0 ){55 lr.end_transaction("shiwu", lr.PASS);56 }else{57 lr.end_transaction("shiwu", lr.FAIL);58 }59 60 //if(set.next() == true ){61 62 // lr.end_transaction("shiwu", lr.PASS);63 64 //}else{65 66 // lr.end_transaction("shiwu", lr.FAIL);67 68 // }69 //列印擷取到的資料70 // while(set.next()){71 // int id = set.getInt("id");72 // System.out.println("id"+id);73 // }74 return 0;75 }// end of action76 77 public int end() throws Throwable {78 //關閉掉ResultSet,PreparedStatement,Connection79 //set.close();//更新,刪除和插入用不到set.close()把它去掉80 ps.close();81 conn.close();82 return 0;83 }// end of end84 85 /* //程式去執行的入口,在LR中不用寫86 public static void main(String[] args) throws Throwable{87 Actions actions = new Actions();88 actions.init();89 actions.action();90 actions.end();91 }92 */93 94 }
以上為java vuser增刪改查的指令碼,其中明確寫出來 查和改 的樣本。
注釋:
1、設定運行查看是否存在java的classpath
2、設定運行所需要的JDBC包並添加進來
3、附一張說明,如所示:
LR介面測試---Java Vuser之增刪改查