java基础

      刚学JAVA,属菜鸟级别人物,想开发一个WEB应用程序,但是java连接mysql不会,于是在网上搜了一堆,最后使用了我认为最简单的,做了一个简单的登陆模块,下面我把我写的源程序提供出来,方便初学者学习。

     首先,我们先建一个title.jsp页面,代码如下:

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 1<%@ page language="java" contentType="text/html; charset=UTF-8"
 2    pageEncoding="UTF-8"%>
 3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4<html>
 5<head>
 6<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7<title>Insert title here</title>
 8<style type="text/css">
 9ExpandedBlockStart.gifContractedBlock.gif.txt {
10    BORDER-RIGHT: #ffffff 1px groove;
11    BORDER-TOP: #ffffff 1px groove;
12    FONT: 12px Verdana, Geneva, sans-serif;
13    BORDER-LEFT: #ffffff 1px groove;
14    WIDTH: 100px;
15    COLOR: #000000;
16    BORDER-BOTTOM: #ffffff 1px groove;
17    HEIGHT: 18px;
18    BACKGROUND-COLOR: #DFF1F9
19}

20
21ExpandedBlockStart.gifContractedBlock.gif.tableborder1 {
22    BORDER: #ffffff 1px groove;
23    BACKGROUND-COLOR: #DFF1F9
24    FONT: 12px Verdana, Geneva, sans-serif;
25    COLOR: #000000;
26    BACKGROUND-COLOR: #DFF1F9
27}

28</style>
29</head>
30<body>
31<%
32    boolean isLog = false;
33ExpandedBlockStart.gifContractedBlock.gif    try {
34        isLog = ((String) session.getAttribute("isLog")).equals("1");
35ExpandedBlockStart.gifContractedBlock.gif    }
 catch (Exception e) {
36
37    }

38%>
39<table width="842" align="center" cellpadding="3" cellspacing="1"
40    class="">
41    <tr>
42        <td width="832" class="tableborder1">:: <a href="index.jsp"
43            target="_top">首页</a> <a href="mail/index.jsp" target="_top">邮件</a> <a
44            href="product/index.jsp" target="_top">商品搜索</a> <a
45            href="jive/index.jsp" target="_top">论坛</a> <%
46ExpandedBlockStart.gifContractedBlock.gif     if (isLog) {
47 %><a href="logout.jsp" target="_top">注销</a> <%
48     }

49 %>
50        </td>
51    </tr>
52    <tr>
53        <td width="832" class="tableborder1">
54        <%
55ExpandedBlockStart.gifContractedBlock.gif            if (isLog) {
56        %> 欢迎光临! <%=session.getAttribute("name")%>,您是第 <%=session.getAttribute("userLogCount")%>
57        次登录,您上次登录的时间是: <%=session.getAttribute("userLastLogTime")%> <%
58ExpandedBlockStart.gifContractedBlock.gif     }
 else {
59 %>
60        <form name="form1" method="post" action="login.jsp">
61        &nbsp;&nbsp;&nbsp; 用户名 <input type="text" name="userId" size="15"
62            class="txt"> 密码 <input type="password" name="password"
63            size="15" class="txt">
64            <input type="submit" name="Submit" value="确定">
65            </form>
66        <%
67            }

68        %>
69        </td>
70    </tr>
71</table>
72</body>
73</html>

 

isLog是为了记录用户是否登陆;

session.getAttribute("name"),

session.getAttribute("userLogCount"),

session.getAttribute("userLastLogTime"),这三个是获取从数据库中读取出来的用户名、登陆次数、上次登陆的时间,显示在页面上,具体与数据的操作会在后面谈到。

第一个页面建立完成,也相当于我们的外表收拾好了,那我们开始展示自己吧。

 

建立login.jsp页面,该页面用于与数据库操作,代码如下:

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 1<%@ page language="java" contentType="text/html; charset=UTF-8"
 2    pageEncoding="UTF-8"%>
 3<%@ page import="java.sql.*"%>
 4<%@ page import="connection.*"%>
 5<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6<html>
 7<head>
 8<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9<title>Insert title here</title>
10</head>
11<body>
12<%
13    //获取用户输入的数据
14    String id = request.getParameter("userId");
15    String psw = request.getParameter("password");
16ExpandedBlockStart.gifContractedBlock.gif    try {
17        //初始化DBcon类
18        DBcon con = new DBcon();
19        //调用类DBcon的Query方法,用于查询数据
20        ResultSet rs = con
21                .Query("select * from user_info where userID='" + id
22                        + "' and password='" + psw + "'");
23        session.setAttribute("isLog"new String("0"));
24
25ExpandedSubBlockStart.gifContractedSubBlock.gif        if (!rs.next()) {
26            //登陆失败,重新登陆
27            response.sendRedirect("index.jsp");
28            //调用类DBcon的Release方法,用于关闭所有连接
29            con.Release();
30ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 else {
31            //登陆成功,获取数据
32            session.setAttribute("id", rs.getInt("ID"));
33            session.setAttribute("name", rs.getString("userID"));
34            session.setAttribute("email", rs.getString("email"));
35            //设置isLog值为1,表示用户登陆成功
36            session.setAttribute("isLog"new String("1"));
37            //获取用户登陆次数
38            int count = rs.getInt("userLogCount");
39            session.setAttribute("userLogCount"new Integer(count));
40            //用户登陆成功,用户登陆次数加1
41            count++;
42            session.setAttribute("userLastLogTime", rs
43                    .getString("userLastLogTime"));
44            java.util.Date time1 = new java.util.Date();
45            String sqltime = new Timestamp(time1.getTime()).toString();
46            //更新用户登陆次数及最后登陆时间
47            con.Update("update user_info set userLogCount=" + count
48                    + ",userLastLogTime='" + sqltime
49                    + "' where userID='" + id + "'");
50            //调用类DBcon的Release方法,用于关闭所有连接
51            con.Release();
52            //登陆成功页面显示
53            response.sendRedirect("index.jsp");
54        }

55ExpandedBlockStart.gifContractedBlock.gif    }
 catch (Exception e) {
56        out.println(e.getLocalizedMessage());
57    }

58%>
59</body>
60</html>

 

上面基本上展示了你的实力了,但是是不是缺少点东西呢,也许你会纳闷我的数据库连接在哪里?呵呵…… 不急,下面就将为你奉上。不知道你有没有注意到上面的DBcon类,那个就是了!

建立一个connection包,然后在里面新建一个类:DBcon

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 1package connection;
 2
 3import java.sql.*;
 4
 5ExpandedBlockStart.gifContractedBlock.gifpublic class DBcon {
 6    ResultSet rs = null;
 7    Connection con = null;
 8    Statement stmt = null;
 9
10    // get connection
11ExpandedSubBlockStart.gifContractedSubBlock.gif    public Statement getCon() {
12
13ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
14                        //使用jdbc-ODBC桥连接
15            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
16ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (ClassNotFoundException e) {
17            System.out.println(e.getLocalizedMessage());
18        }

19
20ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
21                        //建立数据连接
22            con = DriverManager
23                    .getConnection(
24                            "jdbc:odbc:Driver={SQL Server};Server=.;DataBase=Shoppings",
25                            """");
26            stmt = con.createStatement();
27
28ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SQLException e) {
29            System.out.println(e.getLocalizedMessage());
30        }

31
32        return stmt;
33    }

34
35    // query user_info
36ExpandedSubBlockStart.gifContractedSubBlock.gif    public ResultSet Query(String Strsql) {
37        stmt = getCon();
38ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
39            rs = stmt.executeQuery(Strsql);
40ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SQLException e) {
41            System.out.println(e.getLocalizedMessage());
42        }

43        return rs;
44    }

45
46    // update data
47ExpandedSubBlockStart.gifContractedSubBlock.gif    public void Update(String Strsql) {
48        stmt = getCon();
49ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
50            stmt.execute(Strsql);
51ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SQLException e) {
52            System.out.println(e.getLocalizedMessage());
53        }

54    }

55
56    //close all connection
57ExpandedSubBlockStart.gifContractedSubBlock.gif    public void Release() {
58ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
59            rs.close();
60ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SQLException e) {
61            System.out.println(e.getLocalizedMessage());
62        }

63ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
64            stmt.close();
65ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SQLException e) {
66            System.out.println(e.getLocalizedMessage());
67        }

68ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
69            con.close();
70ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SQLException e) {
71            System.out.println(e.getLocalizedMessage());
72        }

73    }

74}

75

 

上面就是我封装的数据库操作类,功能不多,如果需要请自己扩充。好了,基本完成,那我们就来建议一个页面来显示吧。

建立一个index.jsp页面

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<%@ 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>
<jsp:include page="title.jsp" />
    
<center>
        
--- 主页 ---
    
</center>
</body>
</html>

 

这个就是我们的主页了。
<jsp:include page="title.jsp" />是为了把我们在前面所建立的title页面导进来。别忙着调试,不知道你在看login.jsp页面的时候有没有注意到我应用两个package,一个是sql,一个是我们自己建立的包connection,如果想与数据库交往sql是必须的。在DBcon类中我们也引用到了sql。

好了,如果你环境是配置成功的,现在你可以跑跑你的新作了。是不是发现有点不足呢,登陆而不能注销的连接,用户是不是很郁闷,所以,我们还差一个logout.jsp。代码很简单

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<%@ 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>
    
<center>
        
<%
        session.invalidate();
        response.sendRedirect(
"index.jsp");
        
%>
        
<br>
        
<br>
        正在注销……
    
</center>
</body>
</html>

 

好了,现在我们的小程序基本上结束了,当然这只是为新手菜鸟级别的人物所准备的,方便新手们了解页面之前是如何传值和与数据库进行交往的,也是为了给自己作笔记,所以请高手们多多指正。

转载于:https://www.cnblogs.com/blueskyc/archive/2008/08/07/1263138.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值