标签:
在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 init 18 19 public int action() throws Throwable { 20 //2连接mysql,导入java.sql.Connection 21 Connection conn = DriverManager.getConnection(url,username,password); 22 //3获取sql执行器的类,导入java.sql.preparedstatement 23 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,Connection 32 set.close(); 33 ps.close(); 34 conn.close(); 35 return 0; 36 }// end of action 37 38 public int end() throws Throwable { 39 40 return 0; 41 }// end of end 42 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
一切准备就绪,接下来看是否查询到数据,如下图所示有数据返回证明脚本跑通:
本文的脚本只适用于调试用,因为还有需要优化的地方,比如添加参数化、事务、检查点等。
标签:
原文地址:http://www.cnblogs.com/hanxiaobei/p/5361790.html