JDBC查询

BaseDB类:

package com.framework.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class BaseDB {

/*private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=qzw";
private static final String USER = "sa";
private static final String PWD = "sa";*/

public Connection GetConnection() throws IOException{
Connection connection=null;
/*Class.forName(DRIVER).newInstance();
connection= DriverManager.getConnection(URL,USER,PWD);*/

Properties properties=new Properties();
InputStream is=this.getClass().getResourceAsStream("/db.properties");
properties.load(is);
String driver=properties.getProperty("driver");
String url=properties.getProperty("url");
String user=properties.getProperty("user");
String pwd=properties.getProperty("password");
try {
Class.forName(driver);
connection=DriverManager.getConnection(url,user,pwd);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
return connection;
}
/**
* 关闭关键字连接
* @param rSet
* @param pStatement
* @param connection
*/
public static void CloseConnection(Connection connection){
try {
if (connection!=null&&!connection.isClosed()) {
connection.close();
connection=null;

}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

}
}


SQLCommandBean类
package com.framework.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;

public class SQLCommandBean {
private Connection connection;
private String sqlValue;
private List values;


public void setConnection(Connection connection) {
this.connection = connection;
}

public void setSqlValue(String sqlValue) {
this.sqlValue = sqlValue;
}

public void setValues(List values) {
this.values = values;
}

public Result executeQuery() throws SQLException
{
PreparedStatement pStatement=null;
Statement stmt=null;
Result result=null;
ResultSet rSet=null;
try {
if(values != null && values.size()>0)
{
pStatement=connection.prepareStatement(sqlValue);
setValues(pStatement,values);
rSet=pStatement.executeQuery();
}else {
stmt=connection.createStatement();
rSet=stmt.executeQuery(sqlValue);
}
result = ResultSupport.toResult(rSet);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally
{
closeAll(rSet,stmt,pStatement);

}

return result;
}

public int executeUpdate() {
int noOfRows=0;
ResultSet rSet=null;
PreparedStatement pStatement=null;
Statement stmt=null;
try {
if(values !=null && values.size()>0)
{
pStatement=connection.prepareStatement(sqlValue);
setValues(pStatement, values);
noOfRows=pStatement.executeUpdate();
}else {
stmt=connection.createStatement();
noOfRows=stmt.executeUpdate(sqlValue);
}

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally
{
closeAll(rSet, stmt, pStatement);
}
return noOfRows;
}

public void setValues(PreparedStatement pStatement,List values) throws SQLException
{
for (int i = 0; i < values.size(); i++) {
Object v=values.get(i);
pStatement.setObject(i+1, v);
}
}


public void closeAll(ResultSet rSet, Statement stmt,PreparedStatement pStatement) {
try {
if (rSet!=null) {
rSet.close();
rSet=null;

}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

try {
if(stmt!=null)
{
stmt.close();
stmt=null;
}
} catch (Exception e) {
// TODO: handle exception
}
try {
if (pStatement!=null) {
pStatement.close();
pStatement=null;

}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();

}
}
}

JSP result.jsp 测试调用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@include file="SQLCommand.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>Result</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<%
String content = request.getParameter("content");
Connection conn =null;
Result rs =null;
List values =null;
String sqlValue =null;
try{
conn = getConnection();
sqlValue ="select * from user";
sqlValue +=" where username like ?";
values = new ArrayList();
values.add(content);
this.setConnection(conn);
this.setSqlValue(sqlValue);
this.setValues(values);
rs = this.executeQuery();
}catch(Exception ex)
{
ex.printStackTrace();
System.out.println("查询出错了。。。");
}finally
{
closeConnection(conn);
}
for(int i= 0; i<rs.getRowCount(); i++ )
{
Map map = rs.getRows()[i];
System.out.println(map.get("username")+" " + map.get("email"));
}
%>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的海滨体育馆管理系统,源码+数据库+毕业论文+视频演示 本基于Spring Boot的海滨体育馆管理系统设计目标是实现海滨体育馆的信息化管理,提高管理效率,使得海滨体育馆管理工作规范化、高效化。 本文重点阐述了海滨体育馆管理系统的开发过程,以实际运用为开发背景,基于Spring Boot框架,运用了Java技术和MySQL作为系统数据库进行开发,充分保证系统的安全性和稳定性。本系统界面良好,操作简单方便,通过系统概述、系统分析、系统设计、数据库设计、系统测试这几个部分,详细的说明了系统的开发过程,最后并对整个开发过程进行了总结,实现了海滨体育馆相关信息管理的重要功能。 本系统的使用使管理人员从繁重的工作中解脱出来,实现无纸化办公,能够有效的提高海滨体育馆管理效率。 关键词:海滨体育馆管理,Java技术,MySQL数据库,Spring Boot框架 本基于Spring Boot的海滨体育馆管理系统主要实现了管理员功能模块和学生功能模块两大部分,这两大功能模块分别实现的功能如下: (1)管理员功能模块 管理员登录后可对系统进行全面管理操作,包括个人中心、学生管理、器材管理、器材借出管理、器材归还管理、器材分类管理、校队签到管理、进入登记管理、离开登记管理、活动预约管理、灯光保修管理、体育论坛以及系统管理。 (2)学生功能模块 学生在系统前台可查看系统信息,包括首页、器材、体育论坛以及体育资讯等,没有账号的学生可进行注册操作,注册登录后主要功能模块包括个人中心、器材管理、器材借出管理、器材归还管理、校队签到管理、进入登记管理、离开登记管理、活动预约管理。
毕业设计,基于SpringBoot+Vue+MySQL开发的学科竞赛管理系统,源码+数据库+毕业论文+视频演示 随着国家教育体制的改革,全国各地举办的竞赛活动数目也是逐年增加,面对如此大的数目的竞赛信息,传统竞赛管理方式已经无法满足需求,为了提高效率,竞赛管理系统应运而生。 本学科竞赛管理系统以实际运用为开发背景,基于Spring Boot框架、Vue框架,运用了Java语言和MySQL数据库进行开发设计,充分保证系统的安全性和稳定性。本系统界面良好,操作简单方便,通过系统概述、系统分析、系统设计、数据库设计、系统测试这几个部分,详细的说明了系统的开发过程,最后并对整个开发过程进行了总结,实现了学科竞赛管理的重要功能。 本学科竞赛管理系统运行效果稳定,操作方便、快捷,界面友好,是一个功能全面、实用性好、安全性高,并具有良好的可扩展性、可维护性的学科竞赛管理平台。 关键词:学科竞赛管理,Java语言,MySQL数据库,Vue框架 此学科竞赛管理系统的设计与实现功能分析主要分为管理员功能模块和学生功能模块两大模块,下面详细介绍这两大模块的主要功能: (1)管理员:管理员登陆后可对系统进行全面管理,管理员主要功能模块包括个人中心、学生管理、教师管理、赛项信息管理、赛项报名管理、奖项统计管理、管理员管理以及系统管理,管理员实现了对系统信息的查看、添加、修改和删除的功能。 (2)学生:学生进入本学科竞赛管理系统的设计与实现前台可查看系统信息,包括首页、赛项信息以及新闻资讯等,注册登录后主要功能模块包括个人中心和赛项报名管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值