2008年5月15日

程序实现目标:写到数据库即可
目录结构:


UserBean.java
package com.study.bean;

public class UserBean {
    
    
private String username;
    
private String password;
    
public String getUsername() {
        
return username;
    }
    
public void setUsername(String username) {
        
this.username = username;
    }
    
public String getPassword() {
        
return password;
    }
    
public void setPassword(String password) {
        
this.password = password;
    }

    

}
UserDao.java
package com.study.dao;

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

import com.study.bean.*;
import com.study.db.DBHelper;

public class UserDao {
    
/**
     * 
     * 增加用户
     *
     
*/

    
public void setUser(UserBean tuser)
    {
        String sql 
= "INSERT INTO T_USER VALUES(null,?,?)";// USER_NAME=?,USER_PWD=?;
        PreparedStatement pstat = null;
        Connection conn 
= new DBHelper().getConn();
        
try {
            pstat 
= conn.prepareStatement(sql);
          
            pstat.setString(
1, tuser.getUsername());
            pstat.setString(
2, tuser.getPassword());

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

        } finally {
            
try {
                
if (conn != null)
                    conn.close();
                
if (pstat != null)
                    pstat.close();
            } 
catch (SQLException e) {
                
// TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
   

    }
}
DBHelper.java
package com.study.db;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
 * 
 * 数据库连接类
 *
 
*/
public class DBHelper {
    
private String driver = "org.gjt.mm.mysql.Driver";
    
private String url = "jdbc:mysql://localhost:3306/first";
    
private String user = "root";
    
private String pwd = "0433gygy";
    
    
public Connection getConn(){
        Connection conn 
= null;
        
try {
            Class.forName(driver);
            conn 
= DriverManager.getConnection(url, user, pwd);
        } 
catch (ClassNotFoundException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        } 
catch (SQLException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        
return conn;
    }
    
public static void main(String[] args) {
        System.out.println(
"DBHelper.main()" + new DBHelper().getConn());
    }
}
Register.java
package com.study.servlet;


import javax.servlet.http.*;
import javax.servlet.*;

import java.io.*;

import com.study.bean.*;
import com.study.dao.*;

public class Register extends HttpServlet{
    
public void doGet(HttpServletRequest request,HttpServletResponse response)
    
throws ServletException,IOException
    {    
        String un 
= request.getParameter("username");
        String pd 
= request.getParameter("password");
        
        
        UserBean Tuser 
= new UserBean();
        Tuser.setPassword(pd);
        Tuser.setUsername(un);
//        PrintWriter out = response.getWriter();
//        out.println("<html>");
//        out.println("<marquee>"+Tuser.getUsername()+"</marquee>");
//        out.println("</html>");
        UserDao dao = new UserDao();    
        dao.setUser(Tuser);
        
    }

     
public void doPost(HttpServletRequest request, HttpServletResponse response)
     
throws ServletException, IOException
   {
       doGet(request, response);
        
   }
}
web.xml
<servlet>
    
<servlet-name>first</servlet-name>
    
<servlet-class>com.study.servlet.Register</servlet-class>
  
</servlet>
  
<servlet-mapping>
    
<servlet-name>first</servlet-name>
    
<url-pattern>/first</url-pattern>
   

  
</servlet-mapping>
reg.jsp
<body>
    
<form action="first" method = "post">
    username:
<input type="text" name="username">
    password:
<input type="password" name="password">
    
<input type="submit" >
    
</form>
  
</body>

在reg.jsp 自己随便提交个表单.
程序很简单,没什么东西,结果就是写不到数据库,整个程序运行没有报错!望高手指点一下哪边有问题了.



posted @ 2009-04-02 20:16 yxc 阅读(470) | 评论 (4)编辑 收藏

Given:
When //do Stuff  is reached,how many objects are eligible for GC.
class CardBoard{
     Short story 
= 5;
     CardBoard go(CardBoard cb)
{
            cb 
= null;
            
return cb;
          }


public static void main(String[] args){
    CardBoard c1 
= new CardBoard();
    CardBoard c2 
= new CardBoard();
    CardBoard c3 
= c1.go(c2);
    c1
=null;
    
//do Stuff
   }

}

高手帮解释一下,谢谢了.

posted @ 2008-09-23 00:41 yxc 阅读(343) | 评论 (3)编辑 收藏

第一次用,玩一下!
<body>
    
<textarea rows="6" cols="30">
   
<%  
    
int t=1;
    
for(int i=0;i<4;i++)
    {
       
for(int j=0;j<=i;j++)
          out.print(t);
       out.println();
         
    }
%>
 
    
</textarea>

  
</body>

输出:
1
11
111
1111

posted @ 2008-07-01 01:38 yxc 阅读(512) | 评论 (0)编辑 收藏

谁能发一个这样的系统给我,只要实现,登陆注册,还有适当的管理功能,比如网上图书管理系统,银行管理系统之类的。
我的邮箱是yxc0433@126.com    不胜感激。

posted @ 2008-05-20 14:35 yxc 阅读(830) | 评论 (2)编辑 收藏


今天我用myeclipse做了一个关于struts2的小例子,和大家分享当然工程新建和服务器配置就不多说了!导入stuts2 的 jar包也不多说了!
首先配置web-inf下的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
>
 
<filter>
 
<filter-name>struts2</filter-name>
 
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>




右击src建立并配置struts.xml  

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>
   
<struts>
   
<package name="struts2"  extends="struts-default">
   
<action name="login" class="com.test.action.LoginAction">
   
<result name="input">login2.jsp</result>
   
<result>/result.jsp</result>
   
<result name="failer">/login2.jsp</result>
   
</action>
   
</package>

</struts>



并在src目录下建立包com.test.action,里面新建一个java类LoginAction.java
/*
 *Coryright(c)2008 yxc & njust
 *All rights reserved.
*/
package com.test.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{  //此处继承的父类ActionSupport就是struts2的一个典型应用
    private String username;
    
private String password;
    
public String getUsername()
    {
        
return username;
    }
    
public void setUsername(String username)
    {
        
this.username = username;
    }
    
public String getPassword()
    {
        
return password;
    }
    
public void setPassword(String password)
    {
        
this.password = password;
    }
    
public String execute() throws Exception
    {
        
if("hello".equals(this.getUsername().trim())&&"word".equals(this.getPassword().trim()))
        {
            
return "success";
        }
        
else
        {
            
this.addFieldError("username""username or password error!!!!");
            
return "failer";
        }
    }
    @Override
    
public void validate() {    //验证
       
        
if(null==this.getUsername()|| "".equals(this.getUsername().trim()))
        {
            
this.addFieldError("username""username request");
        }
        
if(null==this.getPassword()||"".equals(this.getPassword().trim()))
        {
            
this.addFieldError("password""password request");
        }
    }
   

}

在webroot下建立如下jsp文件,login2.jsp,result.jsp


login2.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding
="ISO-8859-1"%>
<%@ taglib prefix="s"  uri="/struts-tags" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form  action="login">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit name="submit"></s:submit>
</s:form>
</body>
</html>



result.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'result.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>
    username:${requestScope.username}
<br>
    password:${requestScope.password}
</body>
 
</html>


这样就实现了简单的固定用户登陆,username:hello   password:word  

实现界面







posted @ 2008-05-15 01:12 yxc 阅读(6977) | 评论 (1)编辑 收藏


posts - 5, comments - 10, trackbacks - 0, articles - 0

Copyright © yxc