JAVA 学习日记(5).JSP,JDBC数据库查询

1.JSP
实例1:在网页上显示当前时间

index.html

<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" contentType="text/html" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</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>
    <% SimpleDateFormat format=new SimpleDateFormat("yyy年MM月dd日   hh时mm分ss秒");
    String time=format.format(new Date());
     %>
     <%=time %>
  </body>
</html>

实例2:实现登录功能,并捕获登录信息。

login.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'login.jsp' starting page</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>
    <form action="dologin.jsp" method="post">
        用户名:<input type="text" name="username" /><br>
        密码:<input type="password" name="password" /><br>
        兴趣爱好:<input type="checkbox" name="hobby" value="足球"/>足球
                <input type="checkbox" name="hobby" value="排球"/>排球
                <input type="checkbox" name="hobby" value="篮球"/>篮球<br>
                <input type="submit" value="立即登录">
    </form>
  </body>
</html>
dologin.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'dologi.jsp' starting page</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>
    <%
        request.setCharacterEncoding("utf-8");
        String user=request.getParameter("username");
        String pwd=request.getParameter("password");
        String[] hobby=request.getParameterValues("hobby");
            for(int i=0;i<hobby.length;i++){
            out.println(hobby[i]);
        }
     %>
     <%="用户名:"+user+" "+"密码:"+pwd %>
  </body>
</html>

2.JDBC数据库查询
实例:使用JDBC和SQL语句查询数据库信息

目录结构:
这里写图片描述

数据库信息:
这里写图片描述

DBUtil.java

package com.zrgj.shanji.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 数据库连接
 * @author Administrator
 *
 */
public class DBUtil {
//  导包
    public DBUtil(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

//  创建连接
    public Connection getConn(){
        Connection conn=null;
        try {
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8","root","root");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;

    }

//  关闭资源
    public static void closeAll(ResultSet rs,PreparedStatement pstmt,Connection conn){
        try {
            if (rs!=null) {
                rs.close();
            }

            if(pstmt!=null){
                pstmt.close();
            }

            if (conn!=null) {
                conn.close();
            }

        }catch (SQLException e) {
            e.printStackTrace();
        }
    }   
    }
StudentDao.java

package com.zrgj.shanji.dao;

import com.zrgj.shanji.entity.Student;

public interface StudentDao {
    public Student getStudent(String studynum);
}
StudentDaoImpl.java

package com.zrgj.shanji.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.zrgj.shanji.dao.StudentDao;
import com.zrgj.shanji.entity.Student;
import com.zrgj.shanji.util.DBUtil;

public class StudentDaoImpl implements StudentDao {
    DBUtil db=new DBUtil();
    private Connection conn=null;
    private PreparedStatement pstmt=null;
    private ResultSet rs=null;

    public Student getStudent(String studynum) {
        Student stu=null;
        String sql="select * from student where studynum="+"'"+studynum+"'";
        conn=db.getConn();
        try {
            pstmt=conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while(rs.next()){
                stu=new Student();
                int sid=rs.getInt("sid");
                String sname=rs.getString("sname");
                String spwd=rs.getString("spwd");
                String gender=rs.getString("gender");
                stu.setSid(sid);
                stu.setSname(sname);
                stu.setSpwd(spwd);
                stu.setGender(gender);

            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return stu;
    }       
}
Student.java

package com.zrgj.shanji.entity;

public class Student {
    private int sid;
    private String sname;
    private String spwd;
    private String gender;
    private String studynumString;
    public int getSid() {
        return sid;
    }
    public void setSid(int sid) {
        this.sid = sid;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public String getSpwd() {
        return spwd;
    }
    public void setSpwd(String spwd) {
        this.spwd = spwd;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getStudynumString() {
        return studynumString;
    }
    public void setStudynumString(String studynumString) {
        this.studynumString = studynumString;
    }   
}

StudentTest.java

package test;

import com.zrgj.shanji.dao.StudentDao;
import com.zrgj.shanji.dao.impl.StudentDaoImpl;
import com.zrgj.shanji.entity.Student;

public class StudentTest {
    public static void main(String[] args) {
        StudentDao stuDao=new StudentDaoImpl();
        Student stu=stuDao.getStudent("119120110");

        System.out.println(stu.getSname());
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值