简单的查询数据、连接数据库

工具:postgreSQL,Eclipse
技术:jsp,servlet,jdbc
1.准备数据:

select * from city

这里写图片描述
2.写index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="testServlet" name="test" method="post" >
<input name="testText" type="text" placeholder="搜索城市" value="">
</form>
</body>
</html>

结果如下图:
这里写图片描述
2这时候就需要配置web.xml
结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
    <display-name>testServlet</display-name>
    <servlet-name>testServlet</servlet-name>
    <servlet-class>com.zoey.test.testServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>testServlet</servlet-name>
    <url-pattern>/testServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

3.写testServlet

package com.zoey.test;


import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

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

import com.zoey.bean.testData;
import com.zoey.dao.postgreSQLConn;

public class testServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        String testString = req.getParameter("testText");
        Connection conn = postgreSQLConn.conn();
        Statement st = null;
        ResultSet rs = null;
        ArrayList<testData> data= new ArrayList<testData>();
        try {
            st = conn.createStatement();
            String testS = new String(testString.getBytes("ISO-8859-1"), "utf-8");
            String sql = " select * from city where name='" + testS+"'";
             rs =  st.executeQuery(sql);
             if(rs!=null){
            while(rs.next()){
                testData test = new testData();
                test.setId(rs.getInt(1));
                test.setName(rs.getString(2));
                data.add(test);
                 }
          }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                rs.close();
                st.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            postgreSQLConn.close();
        }
        req.setAttribute("data", data);
        req.getRequestDispatcher("/view.jsp").forward(req, resp);
     }
}

4.写view.jsp

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <table border="1" width="500px">
        <tr>
            <td>序号</td>
            <td>数据</td>
        </tr>
    <c:forEach items="${data}" var="dataTest" varStatus="vs">
        <tr>
            <td>${dataTest.id}</td>
            <td>${dataTest.name}</td>
        </tr>
    </c:forEach>
    </table>
</body>
</html>

最终结果
这里写图片描述
写完了代码,总结一下,所遇到的问题?
第一:

String sql = " select * from city where name='" + testS+"'";

sql语句中出现乱码:
更改如下:

String testS = new String(testString.getBytes("ISO-8859-1"), "utf-8");

第二:

<c:forEach items="${data}" var="dataTest" varStatus="vs">

不是很清楚这个,看[http://blog.csdn.net/u011008029/article/details/49908421]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值