码迷,mamicode.com
首页 > 其他好文 > 详细

RowSet

时间:2015-09-04 19:48:23      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

import java.io.FileInputStream;
import java.util.Properties;

import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;




public class JdbcRowSetTest {
	private String driver;
	private String url;
	private String username;
	private String password;
	public void initParam(String paramFile)throws Exception
	{
		Properties props = new Properties();
		props.load(new FileInputStream(paramFile));
		driver = props.getProperty("driver");
		url = props.getProperty("url");
		username = props.getProperty("username");
		password = props.getProperty("password");
	}
	public void update(String sql)throws Exception
	{
		Class.forName(driver);
		RowSetFactory factory = RowSetProvider.newFactory();
		try(
				JdbcRowSet jdbcRs = factory.createJdbcRowSet();
			)
		{
			jdbcRs.setUrl(url);
			jdbcRs.setUsername(username);
			jdbcRs.setPassword(password);
			jdbcRs.setCommand(sql);
			jdbcRs.execute();
			jdbcRs.afterLast();
			while(jdbcRs.previous())
			{
				System.out.println(jdbcRs.getString(1)
						+"\t"+jdbcRs.getString(2)+"\t"
						+jdbcRs.getString(3)+"\t"
						+jdbcRs.getString(4));
				if(jdbcRs.getInt(1)==100)
				{
					jdbcRs.updateString(2, "灵感");
					jdbcRs.updateRow();
				}
			}
		}
		
	}
	
	public static void main(String[] args) throws Exception{
		JdbcRowSetTest jt = new JdbcRowSetTest();
		jt.initParam("Mysql.ini");
		jt.update("select * from user");
	}
}

 

RowSet

标签:

原文地址:http://www.cnblogs.com/masterlibin/p/4782185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!