java怎么把数据库的数据取出来

java怎么把数据库的数据取出来,第1张

java类

public ArrayList selectall(ActiveForm af){

ArrayList list = new ArrayList()

Connection con = null

PreparedStatement ps = null

ResultSet rs = null

try {

con = DBConnection.getcon()

String selectsql = ""

if(af.getAttitle().equals("所有活动")){

selectsql = "select *,zongshu=(select count(abiaoti) from active) from active,atitle where active.atid=atitle.atid"

}else{

selectsql = "select *,zongshu=(select count(abiaoti) from active) from active,atitle where active.atid=atitle.atid and atitle.attitle='"+af.getAttitle()+"'"

}

ps = con.prepareStatement(selectsql)

rs = ps.executeQuery()

while(rs.next()){

ActiveForm af2 = new ActiveForm()

af2.setAid(rs.getInt("aid"))

af2.setAbiaoti(rs.getString("abiaoti"))

af2.setAtext(rs.getString("atext"))

af2.setAtid(rs.getInt("atid"))

af2.setAcost(rs.getInt("acost"))

af2.setAcity(rs.getString("acity"))

af2.setAcount(rs.getInt("acount"))

af2.setAdate(rs.getString("adate"))

af2.setAlevel(rs.getInt("alevel"))

af2.setAstate(rs.getInt("astate"))

af2.setAttitle(rs.getString("attitle"))

af2.setZongshu(rs.getString("zongshu"))

list.add(af2)

}

} catch (Exception e) {

e.printStackTrace()

}finally{

DBConnection.close(con, ps, rs)

}

return list

}

action内容

public ActionForward activefind(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

ActiveForm activeform = (ActiveForm)form

int acost = activeform.getAcost()

String acity = activeform.getAcity()

int acount = activeform.getAcount()

String adate = activeform.getAdate()

int alevel = activeform.getAlevel()

int astate = activeform.getAstate()

String attitle = request.getParameter("keyword")

StringFormat sf = new StringFormat()

activeform.setAcost(acost)

activeform.setAcity(sf.splitstr(StringFormat.notNull(acity)))

activeform.setAcount(acount)

activeform.setAdate(sf.splitstr(StringFormat.notNull(adate)))

activeform.setAlevel(alevel)

activeform.setAstate(astate)

activeform.setAttitle(sf.splitstr(StringFormat.notNull(attitle)))

ActiveDao ad = new ActiveDao()

ArrayList list = ad.selectall(activeform)

if(list.size()>0){

ActiveForm af = (ActiveForm) list.get(0)

request.getSession().setAttribute("zongshu", af.getZongshu())

request.getSession().setAttribute("list", list)

// request.setAttribute("zongshu", af.getZongshu())

// request.setAttribute("list", list)

return mapping.findForward("yes")

}else{

request.getSession().setAttribute("zongshu", "0")

list = new ArrayList()

request.getSession().setAttribute("list",list)

return mapping.findForward("yes")

}

}

jsp内容(涉及到保密,部分代码)

<%

String zongshu

if (session.getAttribute("zongshu") != null) {

zongshu = (String)session.getAttribute("zongshu")

} else {

zongshu = "0"

}

%>

<div id="h-main-right">

<form id="form1" name="form1" method="post" action="<%=request.getContextPath()%>/ActiveAction.do?activetype=activeselect">

<div id="h-right-top">

<table width="100%" height="30" border="0" cellpadding="0"

cellspacing="0">

<tr>

<td width="40%">

当前共有

<%=zongshu%>

个会员活动

</td>

<td width="60%">

关键字

<label>

<input name="abiaoti" type="text" size="12" class="h-biao" />

城市

<input name="acity" type="text" size="12" class="h-biao" />

<select name="alevel">

<option value="100">

不限等级

</option>

<option value="0">

黑钻活动

</option>

<option value="1">

其他钻活动

</option>

</select>

<input type="submit" name="Submit" value="搜索活动" id="h-anniu" />

</label>

</td>

</tr>

</table>

</div>

<div id="h-right-bottom">

<table width="100%" height="80" border="0" cellpadding="0"

cellspacing="0">

<tr>

<td width="35%" height="25" bgcolor="#F1F5FA">

<span class="STYLE2">主题</span>

</td>

<td width="10%" height="25" bgcolor="#F1F5FA">

<span class="STYLE2">报名费</span>

</td>

<td width="15%" height="25" bgcolor="#F1F5FA">

<span class="STYLE2">活动城市</span>

</td>

<td width="6%" height="25" bgcolor="#F1F5FA">

<span class="STYLE2">人气</span>

</td>

<td width="12%" height="25" bgcolor="#F1F5FA">

<span class="STYLE2">发布日期</span>

</td>

<td width="10%" height="25" bgcolor="#F1F5FA">

<span class="STYLE2">报名权限</span>

</td>

<td width="12%" height="25" bgcolor="#F1F5FA">

<span class="STYLE2">活动状态</span>

</td>

</tr>

<%

ArrayList list = new ArrayList()

if (session.getAttribute("list") != null) {

list = (ArrayList) session.getAttribute("list")

}else{

ActiveDao ad = new ActiveDao()

list = ad.se()

}

if (list.size() >0) {

for (int i = 0i <list.size()i++) {

ActiveForm af = (ActiveForm) list.get(i)

%>

<tr>

<td width="35%" height="25"><a href="">

<%=af.getAbiaoti()%></a>

</td>

<td width="10%" height="25">

<%=af.getAcost()%>

</td>

<td height="25">

<%=af.getAcity()%>

</td>

<td width="6%" height="25">

<%=af.getAcount()%>

</td>

<td width="12%" height="25">

<%=af.getAdate()%>

</td>

<% if(af.getAlevel()==0){%>

<td width="10%" height="25">

<img src="<%=request.getContextPath()%>/images/yes-images/activity-02.jpg" alt="黑钻"/>

</td>

<%} %>

<td width="12%" height="25">

<%

if (af.getAstate() == 0) {

%>

已过期

<%

} else {

%>

正在举办

<%

}

%>

</td>

</tr>

<%

}

}

%>

:在数据库提取部分数据,在JSP上显示的做法如下:思路:1、创建db连接2、创建statement3、执行查询4、遍历结果并展示完整代码如下:<spanstyle="font-size:...

package com.joinmysql.demo  

  

import java.sql.DriverManager  

import java.sql.ResultSet  

import java.sql.SQLException  

import java.sql.Connection  

import java.sql.Statement  

   

public class MysqlDemo {  

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

        Connection conn = null  

        String sql  

        // MySQL的JDBC URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称?参数=值  

        // 避免中文乱码要指定useUnicode和characterEncoding  

        // 执行数据库 *** 作之前要在数据库管理系统上创建一个数据库,名字自己定,  

        // 下面语句之前就要先创建javademo数据库  

        String url = "jdbc:mysql://localhost:3306/test?user=root&password=jiangwei&useUnicode=true&characterEncoding=UTF8"  

        try {  

            // 之所以要使用下面这条语句,是因为要使用MySQL的驱动,所以我们要把它驱动起来,  

            // 可以通过Class.forName把它加载进去,也可以通过初始化来驱动起来,下面三种形式都可以  

            Class.forName("com.mysql.jdbc.Driver")// 动态加载mysql驱动  

            // or:  

            // com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver()  

            // or:  

            // new com.mysql.jdbc.Driver()  

   

            System.out.println("成功加载MySQL驱动程序")  

            // 一个Connection代表一个数据库连接  

            conn = DriverManager.getConnection(url)  

            // Statement里面带有很多方法,比如executeUpdate可以实现插入,更新和删除等  

            Statement stmt = conn.createStatement()  

            sql = "create table student(NO char(20),name varchar(20),primary key(NO))"  

            int result = stmt.executeUpdate(sql)// executeUpdate语句会返回一个受影响的行数,如果返回-1就没有成功  

            if (result != -1) {  

                System.out.println("创建数据表成功")  

                sql = "insert into student(NO,name) values('2012001','陶伟基')"  

                result = stmt.executeUpdate(sql)  

                sql = "insert into student(NO,name) values('2012002','周小俊')"  

                result = stmt.executeUpdate(sql)  

                sql = "select * from student"  

                ResultSet rs = stmt.executeQuery(sql)// executeQuery会返回结果的集合,否则返回空值  

                System.out.println("学号\t姓名")  

                while (rs.next()) {  

                    System.out.println(rs.getString(1) + "\t" + rs.getString(2))// 入如果返回的是int类型可以用getInt()  

                }  

            }  

        } catch (SQLException e) {  

            System.out.println("MySQL *** 作错误")  

            e.printStackTrace()  

        } catch (Exception e) {  

            e.printStackTrace()  

        } finally {  

            conn.close()  

        }  

   

    }  

   

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10044531.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存