JSTL获取数据库中的信息

运行环境

  • Tomcat-7.0.35[请先保持关闭状态]
  • MySQL5.5.29[请先保持关闭状态]
  • [Eclipse]Version: Juno Service Release 1
  • Build id: 20120920-0800
  • Fedora 18 /32

此程序在我的平台上可以运行,因此系统安装在虚拟机上,所以也需要保证虚拟机外的系统也可访问.此操作涉及到防火墙操作[文章]

配置数据库

请确认你已经安装Mysql数据库,并已经初始化完成[MySQL启动]

之后,可以执行下面的命令:

//[SHELL]-->表示在Linux终端执行的命令
//[MySQL]-->表示在MySQL中执行的命令

//[SHELL] vim shop.sql
//复制下面的内容,粘贴到里面
DROP DATABASE DEMO;
CREATE DATABASE DEMO;
USE DEMO;
create table SHOP1063 (
id int not null auto_increment,
name varchar(20) not null ,
age int not null ,
primary key(id)
);
//[SHELL]:wq!+ENTER
[SHELL]mysql -u root -p
[SHELL]password:[键入你的密码,之后回车]
[MySQL]:source shop.sql
//如下提示表示创建成功.
//Query OK, 1 row affected (0.03 sec)

//Query OK, 1 row affected (0.00 sec)

//Database changed
//Query OK, 0 rows affected (0.15 sec)

利用Java向MySQL插入数据

在Eclipse中新建Java项目,名为:InsertData,然后复制下面的代码,并执行[返回1表示插入成功]:

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
* insert Data
*
* @author puruidong
* @version 2013.7.29
*
*/
public class InsertData {

/*
* !!!CREATE DATABASE AND CREATE TABLE.
* MYSQL.
*

DROP DATABASE DEMO;
CREATE DATABASE DEMO;
USE DEMO;
create table SHOP1063 (
id int not null auto_increment,
name varchar(20) not null ,
age int not null ,
primary key(id)
);
*/

private static final String DRIVER = "com.mysql.jdbc.Driver";
private static String URL = "jdbc:mysql://localhost:3306/DEMO";
private static String USER = "root";
private static String PASSWORD = "dcxy5201314";
private static Statement st = null ;

private InsertData(){
getStat();
}

/**
* GET Statement Object.
* @return Statement Object.
*/

private Statement getStat(){
try{
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
st = conn.createStatement();

}catch(SQLException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return st;
}

/**
* inser Data.
* @return
* @throws SQLException
*/
private int InsertDatas(int i)throws SQLException{
int result = 0 ;
StringBuffer sql = new StringBuffer();
sql.append(" INSERT INTO SHOP1063 ");
sql.append(" (name,age) ");
sql.append(" VALUES ");
sql.append(" ( 'test"+i+"',");
sql.append(i+");");
result = st.executeUpdate(sql.toString());
return result;
}

public static void main (String [] args){
try{
InsertData id = new InsertData();
int result=0;
for (int i=0;i<30;i++){ result=id.InsertDatas(i); } System.out.println(result); }catch(Exception e){ e.printStackTrace(); } } }

一旦数据插入完成,就可以进行下面的步骤了.

tomcat
image-1908

JSTL处理数据

新建一个动态WEB项目,名称:Test1063

从数据库中获取数据

首先创建一个bean,名称:ShopTest.
然后粘贴以下代码:

package bean;

public class ShopTest {

private int id ;
private String name;
private int age ;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

在bean包下创建一个名称为:ConnDB.Properties的文件[连接字符串,用户名,密码改成你自己的.]:

CONN_DB_URL=jdbc:mysql://localhost:3306/DEMO
CONN_DB_USER=root
CONN_DB_PASSWORD=password

保存.
创建类:CONNDB用来对数据库的连接进行处理.

package bean;

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

public class CONNDB {

private final static String DRIVER = "com.mysql.jdbc.Driver";
private static String URL = null ;
private static String USER = null;
private static String PASSWORD = null ;
private static Statement st = null ;
private static Properties prop = new Properties();
private static String PropFiles = "ConnDB.Properties"; //read Properties

public CONNDB(){
try{
InputStream is = getClass().getResourceAsStream(PropFiles);
prop.load(is);
URL=prop.getProperty("CONN_DB_URL", URL);
USER=prop.getProperty("CONN_DB_USER", USER);
PASSWORD = prop.getProperty("CONN_DB_PASSWORD", PASSWORD);
getStat();
}catch(Exception e){
e.printStackTrace();
}
}

public Statement getStat(){
try{
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
st = conn.createStatement();
}catch(Exception e){
e.printStackTrace();
}
return st ;
}

}

Servlet逻辑处理

在Java Resources-->src-->创建bean包.
然后创建Servlet,名称为:ShopServlet
主要代码如下[注意:导入相关的包]:

//已经省略部分代码

/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String query = request.getParameter("action");
if (query.equals("query")){
this.query(request,response);
}

}

/**
* List.
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List list = new ArrayList();
CONNDB co = new CONNDB();
ResultSet rs = null ;
Statement sts = co.getStat();
try{
rs = sts.executeQuery(" SELECT * FROM SHOP1063 ");
while (rs.next()){
ShopTest st = new ShopTest();
st.setId(rs.getInt("id"));
st.setName(rs.getString("name"));
st.setAge(rs.getInt("age"));
list.add(st);
}
}catch(SQLException e){
e.printStackTrace();
}
request.setAttribute("list", list);
request.getRequestDispatcher("query.jsp").forward(request, response);
}

之后开始配置tomcat的web.xml,代码如下[MyEclipse会自动生成下面的数据,在当前项目的下面]:


ShopServlet
bean.ShopServlet


ShopServlet
/servlet/ShopServlet

JSTL转发数据

新建一个动态WEB项目,之后在WebContext[或者WebRoot]中创建index.jsp:



<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>



首页







展示数据

创建query.jsp用来展示数据,代码如下:


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>



所得数据


ID姓名年龄
${goods.id}${goods.name}${goods.age}



完成工作

  • 启动Tomcat
  • 在终端运行ifconfig获取ip
  • 然后可以在虚拟机外面的系统,访问:[ip:8080/Test1063/index.jsp
  • 页面会跳转到query.jsp并展示从数据库中查询出来的数据.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

fish

sitemap || 淘宝|京东|拼多多|亚马逊| 蜀ICP备20005637号|

Copyright © 2011-2020 酸菜鱼.CODE IS POETRY. BCKF.CN

"Life is short, you need Python"