标签:
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 //定义用户名,密码,连接的URL 10 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.Connection 31 conn = DriverManager.getConnection(url,username,password); 32 //3获取sql执行器的类,导入java.sql.preparedstatement 33 ps = conn.prepareStatement(sql); 34 return 0; 35 }// end of init 36 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,delete 53 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 action 76 77 public int end() throws Throwable { 78 //关闭掉ResultSet,PreparedStatement,Connection 79 //set.close();//更新,删除和插入用不到set.close()把它去掉 80 ps.close(); 81 conn.close(); 82 return 0; 83 }// end of end 84 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、附一张截图说明,如下图所示:
标签:
原文地址:http://www.cnblogs.com/hanxiaobei/p/5361863.html