ResultSet的原理及应用

ResultSet接口基本介绍与应用

基本介绍

1.表示数据库结果集的数据表,通常通过执行查询数据库的语句生成
例如:select语句查询得到的数据表
2.ResultSet对象保持一个光标指向当前的数据行。最初,光标位于第一行之前
3.next方法将光标移动到下一行,并且由于在ResultSet对象中没有更多行时返回false,可以在while循环中使用循环来遍历结果集

应用实例

package Jdbc.myjdbc;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
/**
 * @author zq
 * 演示select语句返回ResultSet,并取出结果
 */
public class ResultSet_ {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        //创建配置文件对象
        Properties properties = new Properties();
        //获取文件中的键值
        properties.load(new FileInputStream("src\\Jdbc\\myjdbc\\mysql.properities"));

        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");

        Class.forName(driver);

        Connection connection = DriverManager.getConnection(url, user, password);
        Statement statement = connection.createStatement();
        //使用SQL语句
        String sql = "select * from actor";
        //执行给定的sql语句,该语句返回单个ResultSet对象
        ResultSet resultSet = statement.executeQuery(sql);
        //使用while循环取出数据
        while (resultSet.next()){
            int id = resultSet.getInt(1);
            String name = resultSet.getString(2);
            String sex = resultSet.getString(3);
            Date date = resultSet.getDate(4);
            System.out.println(id +"\t"+ name + "\t" + sex +"\t"+ date + "\t");
        }
        //关闭资源
        resultSet.close();
        connection.close();
        statement.close();
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
四.实验内容及步骤 设计一个简单的注册流程实现页面,步骤: 设计一个注册的页面,插入一个7行2列的表格,插入一个表单。第一列的内容分别为:用户名,,性别,住址,联系电话,EMAIL地址,密码。 通过表单将注册信息提交:输入JSP代码和JAVA代码,具体操作如下zhuce.jsp 2)在另外一个JSP页面中获得所有的提交信息,并能根据所提交的信息进行相关的判断和跳转等。Tijiao.jsp 代码如下:注册页面zhuce.jsp <html xmlns="http://www.w3.org/1999/xhtml"> <head> body {background-image: url(file:///E|/我的软件/apache-tomcat-5.5.29/webapps/dixie/image/3.jpg); background-color: #00FF66; }.STYLE1 {font-size: 24px} --></style></head> <body> <form action="tijiao.jsp" method="post" name="form1" class="STYLE1" id="form1"> <table width="983" border="1"> <tr><td width="150" height="181"><img src="../image/logo.jpg" width="150" height="168" /></td> <td width="817"><img src="../image/22.jpg" width="817" height="177" /></td> </tr></table> </div> <div align="center"> <table width="974" border="1"> <tr> <td height="39"><div align="center">笛协概况</div></td> <td><div align="center">新闻中心</div></td> <td><div align="center">共享专区</div></td> <td><div align="center">交流专区</div></td> <td><div align="center">笛协文苑</div></td> </tr> </table> <table width="973" height="518" border="1"> <tr> <td width="145"><div align="center">姓名</div></td> <td width="913"> <div align="left"> <input type="text" name="name" />(请用数字或是拼音填写!)</div></td></tr> <tr> <td><div align="center">性别</div></td> <td> <div align="left"> <input type="radio" name="sex" value="nan" /> 男 <input type="radio" name="sex" value="nv" /> 女</div></td> </tr> <tr> <td><div align="center">住址</div></td> <td><div align="left"> <input type="text" name="address" /></div></td></tr> <tr> <td><div align="center">联系电话</div></td> <td><div align="left"> <input type="text" name="tel" /></div></td></tr> <tr> <td><div align="center">Email地址</div></td> <td><div align="left"> <input type="text" name="email" /> <select name="select"> <option value="@126.com">网易126</option> <option value="@163.com">网易163</option><option value="@qq.com">QQ.com</option> <option value="@yahoo.com">yahoo</option></select></div></td></tr> <tr> <td><div align="center">登录密码</div></td> <td><div align="left"><input type="password" name="pwd" /></div></td></tr> <tr><td height="60" colspan="2"><div align="center"> <input type="submit" name="Submit" value="提交" /></div></td></tr></table> </div></form></body></html> 提交页面:tijiao.jsp <html xmlns="http://www.w3.org/1999/xhtml"> <head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <style type="text/css"><!-- body {background-image: url(../image/38.jpg);}--></style></head> <body> <form id="form1" name="form1" method="post" action="shuju.jsp"> <div align="center"> <table width="1080" height="178" border="1"> <tr> <td width="153" height="172"><img src="../image/logo.jpg" alt="13" width="155" height="168" align="baseline" /></td> <td width="911"><img src="../image/22.jpg" alt="12" width="878" height="168" /></td></tr></table> <table width="1080" border="1"> <tr> <td width="188" height="39"><div align="center">笛协概况</div></td> <td width="188"><div align="center">新闻中心</div></td> <td width="188"><div align="center">共享专区</div></td> <td width="188"><div align="center">交流专区</div></td> <td width="298"><div align="center">笛协文苑</div></td></tr> </table> <% String name = request.getParameter("name"); byte a []=name.getBytes("ISO-8859-1"); name=new String(a); String sex = request.getParameter("sex"); String address = request.getParameter("address"); String tel = request.getParameter("tel"); String email = request.getParameter("email"); String select = request.getParameter("select"); String pwd= request.getParameter("pwd"); %> <% ResultSet rs; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn=DriverManager.getConnection( "jdbc:odbc:dixie","","" ); Statement smt = conn.createStatement(); smt.executeUpdate("insert into zhuce values('"+name+"','"+sex+"','"+address+"','"+tel+"','"+email+select+"','"+pwd+"','2')"); { %> <table width="1079" height="336" border="1"> <tr> <td width="145"><div align="center">姓名</div></td> <td width="913"><div align="left"> <%=name%> </div></td> </tr> <tr> <td><div align="center">性别</div></td> <td><div align="left"> <%=sex%> </td></tr> <tr><td><div align="center">住址</div></td> <td><div align="left"> <%=address%> </div></td></tr> <tr> <td><div align="center">联系电话</div></td> <td><div align="left"><%=tel%></div></td> </tr> <tr> <td><div align="center">Email地址</div></td> <td><div align="left"> <%=email%><%=select%></div></td> </tr> <tr><td><div align="center">登录密码</div></td><td><div align="left"><%=pwd%></div></td> </tr></table> <% } conn.close(); //out.print("成功执行"+ i +"条");%> <p> <input type="submit" name="Submit" value="提交" /> </p> </div> </form> </body> </html> 五.实验数据记录 姓名:王意 性别:男 地址:湖北省广水市武胜关镇碾子湾村七组 联系电话:13647217787 Email地址;[email protected] 密码:123 六.问题及体会 1.学会了一些比较简单的JSP编程,和写一些代码 2,知道了request的用法和其使用的原理,即是提交用户信息 3.知道JAVA片段的书写方式和技巧 4.对注册页面有了一定程上的了解并编程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值