LR框架下的jdbcUpdate练习

主要是为了记录一下,有个小坑,大家注意一下,就是你的输入法,参数话的时候,一定要确保你的输入法是英文状态的,要不然,一直会报索引的错。这里用的框架是LR的,下一篇我会写JMeter的。

这是插入方法

package com.niubi.jdbc;

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

		public class JdbcTest {
			String sql = "insert into info (name,age) values (?,?)";
			PreparedStatement ps = null;
			Connection conn = null;
			public int init() throws Throwable{
			  Class.forName("com.mysql.jdbc.Driver");
			  conn =DriverManager.getConnection("jdbc:mysql://你的数据库链接:你的端口/数据库名字", "用户名", "密码");
			ps =conn.prepareStatement(sql);
				return 0;
			}
			public int action() throws Throwable{
				ps.setString(1, "test_"+(int)(Math.random()*100));
				ps.setInt(2, (int)(Math.random()*100));
				int rows  = ps.executeUpdate();
				System.out.println("rows="+rows);
				return 0;
			}
			public int end() throws Throwable{
				ps.close();
				conn.close();
				return 0;
			}
			
			public static void main(String[] args) throws Throwable {
				//创建本类对象
				JdbcTest test = new JdbcTest();
				test.init();
				for(int i=0;i<100;i++) {
					test.action();
				}
				
				test.end();
				
		
			}

		}

这是查询方法

package cn.niubi.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class JdbcQuery {
	String sql = "select * from info where id = ?";
	PreparedStatement ps = null;
	Connection conn = null;
	public int init() throws Throwable{
//		注册驱动
		Class.forName("com.mysql.jdbc.Driver");
		//建立连接
		conn =DriverManager.getConnection("jdbc:mysql://你的数据库链接:你的端口/数据库名字", "用户名", "密码");
		//把sql语句传入到jdbc的类中进行编译,获取到一个专门执行sql语句的类
		ps =conn.prepareStatement(sql);
		return 0;
	}
	public int action() throws Throwable{

		
		ps.setInt(1, (int)(Math.random()*1000));
		//执行sql语句,注意,这里不需要传入sql,因为上一步已经传了
	    ResultSet set = ps.executeQuery();

	    while(set.next()) {
	    	//存在之后,才能取值,不然就会报错,气不气
		    String name = set.getString("name");
	    	System.out.println("name ="+name);
	    }
//		System.out.println("rows="+rows);
		return 0;
	}
	public int end() throws Throwable{
		ps.close();
		conn.close();
		return 0;
	}
	public static void main(String[] args) throws Throwable {
		// TODO Auto-generated method stub
		JdbcQuery test = new JdbcQuery();
		test.init();
		for(int i=0;i<100;i++) {
			test.action();
		}
		
		
		test.end();
	}

}

其实这里只有更新,或者查询。更新包括 增加、删除、修改 也就是preparedstatement里面的update方法
查询就是query方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值