javathead的简单介绍

博主:adminadmin 2022-11-29 14:54:10 53

本篇文章给大家谈谈javathead,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 线程4个方法sleep()、wait()、notify()、notifyAll()中哪些方法会抛出异常?什么异常?

Thead的sleep方法会抛出InterruptedException异常

Object的wait方法会抛出IllegalMonitorStateException,InterruptedException异常

Object的notify方法会抛出IllegalMonitorStateException异常

Object的notifyAll方法会抛出IllegalMonitorStateException异常

用java将数据导出到wps表格中,怎么实现

一:页面图片显示预览:

1)如下图:

2)点击导出按钮后预览:

3)最终生成的excel表格图片预览:

二:代码演示:

说明:执行操作时,请先引进导出excel表格的jar文件包。

找到导出按钮所执行的js方法,在java后天查看该方法的实现即可。

1)jsp代码:

[html] view plaincopyprint?

%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%

%@taglib prefix="s" uri="/struts-tags" %

%

String path = request.getContextPath();

String basePath = request.getScheme() + "://"

+ request.getServerName() + ":" + request.getServerPort()

+ path + "/";

%

html

head

base href="%=basePath%"

title驾校合格率排名/title

link href="jsp/commonstyle/css/tabStyle.css" rel="stylesheet" type="text/css"

link rel="STYLESHEET" type="text/css" href="%=basePath%jsp/hgltj/js/tablesort.css"

script type="text/javascript" src="%=basePath%jsp/system/common/js/publicColor.js"/script

script type="text/javascript" src="jsp/commonstyle/js/js/My97DatePicker/WdatePicker.js" defer="defer"/script

script type="text/javascript" src="%=basePath%jsp/hgltj/js/tablesort.js"/script

script language="JavaScript"

function load()

{

//根据分辨率设置表格大小

maxw=document.getElementById("maintb").offsetWidth;

if(maxw824){//1024分辨率未展开

mainbox.width="98%";

} else if(maxw1013){//1024分辨率展开

mainbox.width="98%";

} else  if(maxw1081){//1280分辨率未展开

mainbox.width="95%";

} else  if(maxw1270){//1280分辨未展开

mainbox.width="95%";

}else{//1280以上分辨展开

mainbox.width="98%";

}

}

/script

script

function overIt(){

var the_obj = event.srcElement;

if(the_obj.tagName.toLowerCase() == "td"){

the_obj=the_obj.parentElement;

the_obj.oBgc=the_obj.currentStyle.backgroundColor;

the_obj.oFc=the_obj.currentStyle.color;

the_obj.style.backgroundColor='#4073C4';

the_obj.style.color='#ffffff';

the_obj.style.textDecoration='underline';

}

}

function outIt(){

var the_obj = event.srcElement;

if(the_obj.tagName.toLowerCase() == "td"){

the_obj=the_obj.parentElement;

the_obj.style.backgroundColor=the_obj.oBgc;

the_obj.style.color=the_obj.oFc;

the_obj.style.textDecoration='';

}

}

function serch(){

document.getElementById("formName").action="%=basePath %hgltj.action?method=getHglpm";

document.getElementById("formName").submit();

}

function tbbt(){

var jzrq=document.getElementById("jzrqId").value;

//var jxmc=document.getElementById("jxmcId").value;

window.open('%=basePath %hgltj.action?method=getHglpmTbtjjxkshgl.jzrq='+jzrq+'tjjxkshgl.zt='+1,'','height=650,width=1250,top=150,left=200,toobar=no,menubar=no,scrollbars=yes,resizable=no,location=no,');

}

function openwd(){

document.getElementById("formName").action="%=basePath %hgltj.action?method=downJxhglPm";

document.getElementById("formName").submit();

}

/script

/head

BODY onLoad="load()" style="background: url(images/cont_bg.gif); background-repeat: repeat-y"

input type="hidden" name="method" value="getDriverInfoList"/

table border="0" width="100%" cellspacing="0" cellpadding="0" id="maintb"

tr

td align="center"

table width="90%"  border="0"  cellspacing="0" cellpadding="0" id="mainbox" style="background:url(jsp/commonstyle/images/usermessage_02.gif); background-repeat:repeat-x;" 

!--宽度可变内容框--

tr 

td width="33" height="27" style=" background:url(jsp/commonstyle/images/usermessage_01.gif); background-position:left; background-repeat:no-repeat;" /td

td width="965" class="style1"font color="black"驾校合格率排名/font/td

td width="14" height="27" style=" background:url(jsp/commonstyle/images/usermessage_03.gif); background-position:right; background-repeat:no-repeat;" /td

/tr

tr

td colspan="3"

!--页面主体内容开始--

!--查询条件--

form action="" name="formName" method="post" style="margin:0px" id="formName" theme="simple"

table width="100%" border="0" cellpadding="0" cellspacing="0"   style="border-collapse:collapse;" id="tj" align="center"

tr align="left"

td class="tjbg1" style="text-align: left"

!-- input type="hidden" id="method"  name="method" value="getHglpm"/ --

统计日期:

input type="text" name="tjjxkshgl.jzrq" id="jzrqId" value="s:property value="tjjxkshgl.jzrq"/" onclick="WdatePicker({skin:'whyGreen',dateFmt:'yyyy-MM'})"/

!-- 驾校名称:

s:select id="jxmcId"  name="tjjxkshgl.jxxh"  value="tjjxkshgl.jxxh" list="schoolList" listKey="jxxh" listValue="jxmc" headerKey="" headerValue="--请选择--" theme="simple"/s:select        --

input name="input" value=" 统 计 " type="button" class="normalbtn" onClick="serch()" style="margin-bottom: 5px" /

input name="input" type="button" value=" 合格率图表 " onclick="tbbt()" class="normalbtn" style="margin-bottom: 5px" /

input id="Button1" type="button" value=" 导 出 " onclick="openwd();" class="normalbtn" style="margin-bottom: 5px" /

/td

/tr

/table!--查询结果--

/form

!--查询结果--

table width="100%" border="0" cellpadding="4" cellspacing="1" bgcolor="#abcfff" id="cxjg" align="center"

thead

tr class="tbtitle"

td width="4%" align="center" class="t1"名次/td

td width="8%" align="center" class="t1"名称/td

td class="t1" width="5%" align="center"科目一/td

td class="t1" width="5%" align="center"科目二/td

td class="t1" width="5%" align="center"科目三/td

td class="t1" width="5%" align="center"平均合格率/td

td class="t1" width="5%" align="center"操作/td

/tr

/thead

s:iterator id="jxhgl" value="jxhelpmList" status="st"

tr class="changeColor" onMouseOver="overIt()" onMouseOut="outIt()" style="cursor: hand" align="center"

tds:property value="#st.index+1"//td

tds:property value="#jxhgl.jxmc"//td

tds:property value="#jxhgl.km1hgl"//td

tds:property value="#jxhgl.km2hgl"//td

tds:property value="#jxhgl.km3hgl"//td

tds:property value="#jxhgl.avghgl"/%/td

td

a href="javascript:" onclick="openWin('%=basePath %hgltj.action?method=getTbForJxxhtjjxkshgl.jxxh=s:property value="#jxhgl.jxxh"/tjjxkshgl.zt=1','',1250,750);"图表/a

/td

/tr

/s:iterator

/table

/table

/td

/tr

/table

/body

/html

2)java代码演示:

[java] view plaincopyprint?

/**

* 驾校合格率导出excel图表

*/

//response.getOutputStream();// 取得输出流

response.reset();// 清空输出流

String tmptitle = "驾校合格率排名"; // 标题

response.setHeader("Content-disposition", "attachment; filename="+new String(tmptitle.getBytes(),"iso8859-1")+".xls");// 设定输出文件头

response.setContentType("application/vnd.ms-excel");// 定义输出类型

wbook = Workbook.createWorkbook(os); // 建立excel文件

WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称

// 设置excel标题

   

//cellFormat.setBackground(Colour.AQUA);

cellFormat.setFont(wfont);

label.setCellFormat(cellFormat);

wsheet.addCell(label);

//wsheet.addCell(new Label(0, 0, tmptitle, wcfFC));

wsheet.setRowView(0,500); //第一行高度

wsheet.mergeCells(0, 0, 6, 1);  //合并单元格(第一列的第一行和第七列的第二行合并)

//wsheet.mergeCells(0, 1, 9, 1);

//      wsheet.mergeCells(0, 2, 0, 4);

//      wsheet.mergeCells(1, 2, 3, 2);

//      wsheet.mergeCells(4, 2, 6, 2);

//      wsheet.mergeCells(7, 2, 9, 2);

wsheet.setColumnView(0,10); //宽度

wsheet.setColumnView(1,25); //宽度

wsheet.setColumnView(2,10); //宽度

wsheet.setColumnView(3,10); //宽度

wsheet.setColumnView(4,10); //宽度

wsheet.setColumnView(5,10); //宽度

// 开始生成主体内容

wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);

wcfFC = new WritableCellFormat(wfont);

wsheet.addCell(new Label(0, 2, "名次",wcfFC));

wsheet.addCell(new Label(1, 2, "驾校名称",wcfFC));

wsheet.addCell(new Label(2, 2, "科目一",wcfFC));

wsheet.addCell(new Label(3, 2, "科目二",wcfFC));

wsheet.addCell(new Label(4, 2, "科目三",wcfFC));

wsheet.addCell(new Label(5, 2, "合格率",wcfFC));

int count=jxhelpmList.size();

if(count0){  ////判断集合是否不为0

TjJxkshgl tjhgl=null;

for(int i=0;ijxhelpmList.size();i++){

tjhgl=(TjJxkshgl)jxhelpmList.get(i);

wsheet.addCell(new Label(0, i+3, (i+1)+""));

wsheet.addCell(new Label(1, i+3, tjhgl.getJxmc()));

wsheet.addCell(new Label(2, i+3, tjhgl.getKm1hgl()));

wsheet.addCell(new Label(3, i+3, tjhgl.getKm2hgl()));

wsheet.addCell(new Label(4, i+3, tjhgl.getKm3hgl()));

wsheet.addCell(new Label(5, i+3, tjhgl.getAvghgl()));

   

}

JAVA中Thead cannot be resolved to a type怎么解决 程序如下

代码编译错误了

Thread t =new Thead(); --------------------后面的改成new Thread(); 少写了个r

Java如何做动态表格?

一、动态加载表格

1.首先在html中为表格的添加位置设置id

即是在html的body标签内部写一个div标签表明表格要添加到此div的内部。如下

div id="tdl"div

2.在javascript中写添加表格的语句

若在当前html文件中,则写在script标签内部,如

代码如下:

script type="text/javascript"

document.getElementById("tbl").innerHTML="tabletrtd/td/tr/table" //此处添加的表格可根据自己需要创建

/script

若是通过引入js文件,则在js文件(假设是test.js)中直接写如下语句

代码如下:

document.getElementById("tbl").innerHTML="tabletrtd/td/tr/table"

然后再引入自己的html文件

代码如下:

script type="text/javascript" src="test.js"/script

二、 动态添加表格行

1.首先在html中为表格行的添加位置设置id,此位置必须是tbody内部(不是特别准确,但根据我的测试就得到此结论,有其他的方法请留言,谢谢),如下

代码如下:

table

thead/thead

tfoottfoot //tfoot与thead是与tbody配套使用,但我在写的时候,没用也可以。

tbody id="rows"/tbody

/table

[\s\S ]*\n

2.在javascript内容中,要先创建行和单元格,再在.tbody中添加行,如下

[code]

row=document.createElement("tr"); //创建行

td1=document.createElement("tr"); //创建单元格

td1.appendChild(document.createTextNode("content")); //为单元格添加内容

row.appendChild(td1); //将单元格添加到行内

document.getElementById("rows").append(row); //将行添加到tbody中

java 用什么实现 FIFO队列?

java使用数据结构来实现FIFO先进先出的队列,实例如下:

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package linkedlisttest;

import java.util.ArrayList;

import java.util.Deque;

import java.util.LinkedList;

import java.util.List;

/**

 *

 * @author Vicky.H

 * @email eclipser@163.com

 */

public class FIFOTest {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        FIFOA fifo = new FIFOImplA(5);

        for (int i = 0; i  20; i++) {

            A a = new A("A:" + i);

            A head = fifo.addLastSafe(a);

            System.out.println(i + "\thead:" + head + "\tsize:" + fifo.size());

        }

        System.out.println("---------------");

        System.out.println("弹出数据");

        ListA polls = fifo.setMaxSize(3);

        for (A a : polls) {

            System.out.println("\thead:" + a);

        }

        

        System.out.println("剩余数据");

        for (A a : fifo) {

            System.out.println("\thead:" + a);

        }

        System.out.println(fifo.size());

    }

}

interface FIFOT extends ListT, DequeT, Cloneable, java.io.Serializable {

    /**

     * 向最后添加一个新的,如果长度超过允许的最大值,则弹出一个 *

     */

    T addLastSafe(T addLast);

    /**

     * 弹出head,如果Size = 0返回null。而不同于pop抛出异常

     * @return 

     */

    T pollSafe();

    /**

     * 获得最大保存

     *

     * @return

     */

    int getMaxSize();

    /**

     * 设置最大存储范围

     *

     * @return 返回的是,因为改变了队列大小,导致弹出的head

     */

    ListT setMaxSize(int maxSize);

}

class FIFOImplT extends LinkedListT implements FIFOT {

    private int maxSize = Integer.MAX_VALUE;

    private final Object synObj = new Object();

    public FIFOImpl() {

        super();

    }

    public FIFOImpl(int maxSize) {

        super();

        this.maxSize = maxSize;

    }

    @Override

    public T addLastSafe(T addLast) {

        synchronized (synObj) {

            T head = null;

            while (size() = maxSize) {

                head = poll();

            }

            addLast(addLast);

            return head;

        }

    }

    @Override

    public T pollSafe() {

        synchronized (synObj) {

            return poll();

        }

    }

    @Override

    public ListT setMaxSize(int maxSize) {

        ListT list = null;

        if (maxSize  this.maxSize) {

            list = new ArrayListT();

            synchronized (synObj) {

                while (size()  maxSize) {

                    list.add(poll());

                }

            }

        }

        this.maxSize = maxSize;

        return list;

    }

    @Override

    public int getMaxSize() {

        return this.maxSize;

    }

}

class A {

    private String name;

    public A() {

    }

    public A(String name) {

        this.name = name;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    @Override

    public String toString() {

        return "A{" + "name=" + name + '}';

    }

}

java数据库操作问题(上一条数据、下一条数据)

给你个思路吧:

lt;%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%gt;

lt;%@ taglib prefix="s" uri="/struts-tags" %gt;

lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%gt;

lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"gt;

lt;htmlgt;

lt;headgt;

lt;base href="lt;%=basePath%gt;"gt;

lt;titlegt;My JSP 'list.jsp' starting pagelt;/titlegt;

lt;meta http-equiv="content-type" content="text/html;charset=utf-8"gt;

lt;meta http-equiv="pragma" content="no-cache"gt;

lt;meta http-equiv="cache-control" content="no-cache"gt;

lt;meta http-equiv="expires" content="0"gt;

lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"gt;

lt;meta http-equiv="description" content="This is my page"gt;

lt;!--

lt;link rel="stylesheet" type="text/css" href="styles.css"gt;

--gt;

lt;stylegt;

.row0{

background: #AABBFF;

}

.row1{

background: #FFAABB;

}

lt;/stylegt;

lt;/headgt;

lt;bodygt;

lt;divgt;lt;span style="color:red;"gt;请用list.action访问该页面lt;/spangt;lt;/divgt;

lt;table width="100%" border="1px solid black" cellpadding="0" cellspacing="0"gt;

lt;captiongt;用户列表及操作lt;/captiongt;

lt;theadgt;

lt;trgt;

lt;th width="20%"gt;IDlt;/thgt;

lt;th width="20%"gt;用户名lt;/thgt;

lt;th width="20%"gt;密码lt;/thgt;

lt;th width="20%"gt;是否可用lt;/thgt;

lt;th width="20%"gt;操作lt;/thgt;

lt;/trgt;

lt;/theadgt;

lt;tbodygt;

lt;s:iterator value="%{users}" status="s"gt;

lt;tr class="row${s.index%2 }"gt;

lt;tdgt;${id }lt;/tdgt;

lt;tdgt;${username }lt;/tdgt;

lt;tdgt;${password }lt;/tdgt;

lt;tdgt;${valid }lt;/tdgt;

lt;tdgt;

lt;a href="delete.action?id=${id }amp;p.page=${p.page }" onclick="return confirm('确定删除?')"gt;删除lt;/agt;

lt;a href="load.action?id=${id }amp;p.page=${p.page }"gt;修改lt;/agt;

lt;/tdgt;

lt;/trgt;

lt;/s:iteratorgt;

lt;/tbodygt;

lt;/tablegt;

lt;divgt;

lt;form action="list.action"gt;

lt;s:if test="%{p.page != 1}"gt;

lt;a href="list.action?p.page=1"gt;首页lt;/agt;

lt;/s:ifgt;

lt;s:elsegt;

lt;a style="color:#ccc;"gt;首页lt;/agt;

lt;/s:elsegt;

lt;s:if test="%{p.page != 1}"gt;

lt;a href="list.action?p.page=${p.page-1 }"gt;上一页lt;/agt;

lt;/s:ifgt;

lt;s:if test="%{p.page != p.totalPage}"gt;

lt;a href="list.action?p.page=${p.page+1 }"gt;下一页lt;/agt;

lt;/s:ifgt;

lt;s:if test="%{p.page != p.totalPage}"gt;

lt;a href="list.action?p.page=${p.totalPage }"gt;末页lt;/agt;

lt;/s:ifgt;

lt;s:elsegt;

lt;a style="color:#ccc;"gt;末页lt;/agt;

lt;/s:elsegt;

lt;spangt;第${p.page }页/共${p.totalPage }页lt;/spangt;

lt;spangt;跳lt;input id="page" name="p.page" style="width:30px;"gt;页lt;/spangt;

lt;input type="submit" value="go"gt;

lt;/formgt;

lt;/divgt;

lt;divgt;

lt;input type="button" value="添加用户" onclick="window.location='add.jsp'"/gt;

lt;/divgt;

lt;/bodygt;

lt;/htmlgt;

package com.action;

import java.util.ArrayList;

import java.util.List;

import com.dao.UserDao;

import com.dao.UserDaoImpl;

import com.opensymphony.xwork2.ActionSupport;

import com.pojo.User;

import com.util.Pagination;

public class UserAction extends ActionSupport{

private UserDao dao = new UserDaoImpl();

private Listlt;Usergt; users = new ArrayListlt;Usergt;();

private User user;

private int id;

private Pagination p = new Pagination();

public String list(){

System.out.println("======================list.action");

try {

System.out.println("list:"+p.getPage()+p.getPageSize());

dao.getMaxPage(p);

users = dao.list(p);

return "success";

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return "failed";

}

public String add(){

System.out.println("======================add.action");

try {

dao.add(user);

return "success";

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return "failed";

}

public String delete(){

System.out.println("======================delete.action");

try {

dao.delete(id);

return "success";

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return "failed";

}

public String load(){

System.out.println("======================load.action");

try {

user = dao.findUserById(id);

return "success";

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return "failed";

}

public String modify(){

System.out.println("======================modify.action");

try {

user.setId(id);//设置user的id为所要修改的id

dao.update(user);

return "success";

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return "failed";

}

public Listlt;Usergt; getUsers() {

return users;

}

public void setUsers(Listlt;Usergt; users) {

this.users = users;

}

public User getUser() {

return user;

}

public void setUser(User user) {

this.user = user;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public Pagination getP() {

return p;

}

public void setP(Pagination p) {

this.p = p;

}

}

package com.util;

/**

* 分页

* @author yun

*

*/

public class Pagination {

private int pageSize = 3;//每页显示几条数据

private int totalPage = Integer.MAX_VALUE;//共多少页

private int page = 1;//第几页

public int getPageSize() {

return pageSize;

}

public void setPageSize(int pageSize) {

if(pageSize lt;= 0){

pageSize = 3;

}

this.pageSize = pageSize;

}

public int getTotalPage() {

return totalPage;

}

public void setTotalPage(int totalPage) {

if(totalPage lt;=0){

totalPage = 1;

}

this.totalPage = totalPage;

//下面的setPage(page)一定要有,因为totalPage是查询出来的,这会影响page的值。

//如:原来有12页数据,现在查询出来的只有2页,那么page大于2的页应该就不存在了

setPage(page);

}

public int getPage() {

return page;

}

public void setPage(int page) {

System.out.println("=========totalPage:"+totalPage);

if(page lt;= 0){

page = 1;

}

if(page gt; totalPage){

page = totalPage;

}

this.page = page;

}

}

package com.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.List;

import com.pojo.User;

import com.util.DBUtil;

import com.util.Pagination;

public class UserDaoImpl implements UserDao {

public void add(User u) throws Exception {

int id = searchMaxId();//获得id值,相当于id = seq.nextval()

System.out.println("获得id值=============="+id);

u.setId(id);

String sql = "insert into s_user(id,username,password,valid) " +

" values(?,?,?,?)";

Connection conn = DBUtil.getConnection();

PreparedStatement pstmt = conn.prepareStatement(sql);

pstmt.setInt(1, u.getId());

pstmt.setString(2, u.getUsername());

pstmt.setString(3, u.getPassword());

pstmt.setString(4, u.getValid());

System.out.println("打印sql:"+sql+"\t参数:["+u.getId()+","+u.getUsername()+","+u.getPassword()+","+u.getValid()+"]");

pstmt.executeUpdate();

DBUtil.close(pstmt);

DBUtil.close(conn);

}

/**

* 查询记录中最大的id,如果没有就默认为1,有就+1返回

* @return

*/

private int searchMaxId() {

int id = 1;

String sql = "select max(id) mid from s_user";

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

try {

conn = DBUtil.getConnection();

stmt = conn.createStatement();

System.out.println("打印sql:"+sql);

rs = stmt.executeQuery(sql);

if(rs.next()){

id = rs.getInt("mid")+1;//有结果,就+1

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

rs.close();

stmt.close();

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

return id;

}

public void delete(int id) throws Exception {

String sql = "delete from s_user where id = ?";

Connection conn = DBUtil.getConnection();

PreparedStatement pstmt = conn.prepareStatement(sql);

pstmt.setInt(1, id);

System.out.println("打印sql:"+sql+"\t参数:["+id+"]");

pstmt.executeUpdate();

DBUtil.close(pstmt);

DBUtil.close(conn);

}

public User findUserById(int id) throws Exception {

User u = null;

String sql = "select * from s_user where id = ?";

Connection conn = DBUtil.getConnection();

PreparedStatement pstmt = conn.prepareStatement(sql);

pstmt.setInt(1, id);

System.out.println("打印sql:"+sql+"\t参数:["+id+"]");

ResultSet rs = pstmt.executeQuery();

if(rs.next()){

u = new User();

u.setId(rs.getInt("id"));

u.setUsername(rs.getString("username"));

u.setPassword(rs.getString("password"));

u.setValid(rs.getString("valid"));

}

DBUtil.close(rs);

DBUtil.close(pstmt);

DBUtil.close(conn);

return u;

}

public Listlt;Usergt; list() throws Exception {

Listlt;Usergt; list = new ArrayListlt;Usergt;();

String sql = "select id,username,password,valid from s_user order by id";

Connection conn = DBUtil.getConnection();

Statement stmt = conn.createStatement();

System.out.println("打印sql:"+sql);

ResultSet rs = stmt.executeQuery(sql);

while(rs.next()){

int id = rs.getInt("id");

String username = rs.getString("username");

String password = rs.getString("password");

String valid = rs.getString("valid");

User u = new User();

u.setId(id);

u.setUsername(username);

u.setPassword(password);

u.setValid(valid);

list.add(u);

}

DBUtil.close(rs);

DBUtil.close(stmt);

DBUtil.close(conn);

return list;

}

public Listlt;Usergt; list(Pagination p) throws Exception {

Listlt;Usergt; list = new ArrayListlt;Usergt;();

String sql = "select * from (select ini.*,rownum rn from " +

"(select id,username,password,valid from s_user order by id) ini " +

"where rownum lt;= ?) where rn gt;= ? ";

int begin = p.getPageSize()*(p.getPage()-1)+1;//10*(3-1)+1=21 1-10 11-20 21-30

int end = p.getPageSize()*p.getPage();//10*3=30

Connection conn = DBUtil.getConnection();

PreparedStatement pstmt = conn.prepareStatement(sql);

pstmt.setInt(1, end);

pstmt.setInt(2, begin);

System.out.println("打印sql:"+sql+"\t参数:["+end+","+begin+"]");

ResultSet rs = pstmt.executeQuery();

while(rs.next()){

int id = rs.getInt("id");

String username = rs.getString("username");

String password = rs.getString("password");

String valid = rs.getString("valid");

User u = new User();

u.setId(id);

u.setUsername(username);

u.setPassword(password);

u.setValid(valid);

list.add(u);

}

DBUtil.close(rs);

DBUtil.close(pstmt);

DBUtil.close(conn);

return list;

}

public void update(User u) throws Exception {

String sql = "update s_user set username=?,password=?,valid=? where id=?";

Connection conn = DBUtil.getConnection();

PreparedStatement pstmt = conn.prepareStatement(sql);

pstmt.setString(1, u.getUsername());

pstmt.setString(2, u.getPassword());

pstmt.setString(3, u.getValid());

pstmt.setInt(4, u.getId());

System.out.println("打印sql:"+sql+"\t参数:["+u.getUsername()+","+u.getPassword()+","+u.getValid()+","+u.getId()+"]");

pstmt.executeUpdate();

DBUtil.close(pstmt);

DBUtil.close(conn);

}

/**

* 设置最大页

* @param p

* @throws SQLException

*/

public void getMaxPage(Pagination p) throws SQLException{

String sql = "select count(*) from s_user";

Connection conn = DBUtil.getConnection();

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(sql);

if(rs.next()){

int s = rs.getInt(1);//一共有多少条数据

int n = p.getPageSize();//每页大小

int t = (s+n-1)/n;

p.setTotalPage(t);//共有多少页

}

DBUtil.close(rs);

DBUtil.close(stmt);

DBUtil.close(conn);

}

/**

* 测试UserDaoImpl

* @param args

* @throws Exception

*/

public static void main(String[] args) throws Exception {

UserDaoImpl impl = new UserDaoImpl();

int size = impl.list().size();

System.out.println("size======"+size);//size======0

}

}

总体来说,这是一个翻页查询问题。

上一条,下一条如果是两个按钮的话,就要写javascript去异步发送请求了,请求时来带上参数,参数为当前的页数减一(上一条),或加一(下一条)。然后就是写查询语句了。

如:

public ListUser list(Pagination p) throws Exception {

ListUser list = new ArrayListUser();

String sql = "select * from (select ini.*,rownum rn from " +

  "(select id,username,password,valid from s_user order by id) ini " +

  "where rownum = ?) where rn = ? ";

int begin = p.getPageSize()*(p.getPage()-1)+1;//10*(3-1)+1=21  1-10 11-20 21-30

int end = p.getPageSize()*p.getPage();//10*3=30

Connection conn = DBUtil.getConnection();

PreparedStatement pstmt = conn.prepareStatement(sql);

pstmt.setInt(1, end);

pstmt.setInt(2, begin);

System.out.println("打印sql:"+sql+"\t参数:["+end+","+begin+"]");

ResultSet rs = pstmt.executeQuery();

while(rs.next()){

 int id = rs.getInt("id");

 String username = rs.getString("username");

 String password = rs.getString("password");

 String valid = rs.getString("valid");

 User u = new User();

 u.setId(id);

 u.setUsername(username);

 u.setPassword(password);

 u.setValid(valid);

 list.add(u);

}

DBUtil.close(rs);

DBUtil.close(pstmt);

DBUtil.close(conn);

return list;

}

同理,还是要去查询,然后得到两条记录,还是要传参数的。

关于javathead和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-29,除非注明,否则均为首码项目网原创文章,转载请注明出处。