rs.last()

package com.runoob.test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.security.action.GetBooleanAction;

/**
* Servlet implementation class Login
*/
@WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;


//JDBC驱动器名称和数据库的URL
static final String JDBC_DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
static final String DB_URL="jdbc:sqlserver://localhost:1433;DatabaseName=ReportServerTempDB";

//数据库的凭据
static final String USER="sa";
static final String PASS="123457";

private Statement stmt;
private Connection conn;
private ResultSet rs;
/**
* @see HttpServlet#HttpServlet()
*/
public Login() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//throw new ServletException("GET method used with " +
//getClass( ).getName( )+": POST method required.");

}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request,response);
String site0 = request.getContextPath() + "/InPut.jsp";
String site1 = new String("http://localhost:8080/Login/Login.jsp");
String userId=request.getParameter("userId");
String pass=request.getParameter("password");
response.setContentType("text/html;charset=UTF-8");
try{
//注册JDBC驱动器
Class.forName(JDBC_DRIVER);

//打开一个连接
conn = DriverManager.getConnection(DB_URL, USER, PASS);

//执行SQL查询
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql = "SELECT userId,password FROM Table_1 where userId ='" + userId +"' and password = '" + pass + "'";
rs = stmt.executeQuery(sql);
rs.last(); //定位到最后一行,即读出到最后一行,否则,计算不出来所有的行数

int rowCount = rs.getRow();


if(rowCount>0)
{

request.getSession().setAttribute("userInfo", userId);
response.sendRedirect(site0);

}
else
{
response.sendRedirect(site1);

}
}
catch(SQLException se){
//处理JDBC错误
se.printStackTrace();
}
catch(Exception e){
//处理Class.forName错误
e.printStackTrace();
}
finally{
//最后是用于关闭资源的块
try{
if(stmt!=null)
stmt.close();
}
catch(SQLException se2){

}//我们不能做什么
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}

try{
if(conn!=null)
conn.close();
}
catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try

}

}

转载于:https://www.cnblogs.com/cyy-13/p/5780216.html

21:08:05 RandomGetRecord. java↵ import java. sql.*;↵ import java. util.*;↵ public class RandomGetRecord {↵ public static void main(String args[]){↵ int wantRecordAmount=10;随机抽取的记录数目↵ Random random=new Random();↵ try{ 【代码1】//加载JDBC-ODBC桥接器↵ }< catch(ClassNotFoundException e){↵ System. out. print(e);↵ }. Connection con;↵ Statement sql;↵ ResultSe t rs;↵ 21:09:00 try{ String uri="jdbc:odbc:dataSource";↵ String id="";↵ String password=””;↵ con=DriverManager. getConnection(uri, id, password);↵ sql= con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,↵ ResultSet.CONCUR_READ_ONLY);↵ rs=【代码2】//sql调用. executeQuery方法查询goods表中的全部记录↵ rs. last(); /将rs的游标移到rs的最后一行↵ _ int count=rs. getRow(); Vector<Integer>vector=new Vector<Integer>0;↵ for(int i=1;i<=count;i++) { ↵ vector. add(new Integer(i));↵ } ↵ int itemAmount=Math . min(wantRecordAmount, count);↵ System. out. println("随机抽取"+itemAmount+"条记录.");↵ double sum =0,n=itemAmount;↵ while(itemAmount>0){↵ int randomIndex =random. nextInt(vector. size());↵ int index=(vector. elementAt(randomIndex)). intValue();↵ 【代码3】//将rs的游标游标移到index↵ String number =rs. getString(1);↵ String name=rs. getString(2);↵ java. util. Date date=rs. getDate(3);↵ double price=rs. getDouble(4);↵ sum=sum+price;↵ itemAmount--; vector. removeElementAt(randomIndex);↵ } con. close();↵ double aver=sum/n;↵ System. out. println("均价:"+aver+"元");↵ } catch(SQLException e){↵ 21:10:55 System.out.println(“”+e);
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值