凑一个热闹
/**
* @author wukai
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JavaStudy extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JLabel jLabel1 = new JLabel();
JPanel jPanel1 = new JPanel();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
GridLayout gridLayout1 = new GridLayout();
JPanel jPanel2 = new JPanel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
GridLayout gridLayout2 = new GridLayout();
JButton jButton1 = new JButton();
public JavaStudy(String s){
super();
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public void jbInit(){
this.getContentPane().setLayout(borderLayout1);
jLabel1.setBorder(BorderFactory.createEtchedBorder());
jLabel1.setToolTipText("");
jLabel1.setDisplayedMnemonic('0');
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("程序事例");
jPanel1.setLayout(gridLayout1);
gridLayout1.setColumns(0);
gridLayout1.setHgap(0);
gridLayout1.setRows(2);
gridLayout1.setVgap(10);
jLabel2.setText("输入");
jLabel3.setText("输出");
jPanel2.setLayout(gridLayout2);
gridLayout2.setRows(2);
gridLayout2.setVgap(10);
jButton1.setText("确定");
jButton1.addActionListener(new JavaStudy_jButton1_actionAdapter(this));
jTextField1.setText("");
jTextField2.setText("");
jPanel1.add(jTextField1, null);
jPanel1.add(jTextField2, null);
this.getContentPane().add(jPanel2, BorderLayout.WEST);
jPanel2.add(jLabel2, null);
jPanel2.add(jLabel3, null);
this.getContentPane().add(jButton1, BorderLayout.SOUTH);
this.getContentPane().add(jLabel1, BorderLayout.NORTH);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
}
public static void main(String[] args) {
JavaStudy javastudy=new JavaStudy("小程序");
javastudy.show();
} void jButton1_actionPerformed(ActionEvent e) {
String b,a;
b="boy";
a="girl";
if (jTextField1.getText().equals(b))
{ jTextField2.setText("男孩");}
if (jTextField1.getText().equals(a) )
{ jTextField2.setText("女孩");}
else
{ jTextField2.setText("错误,无此词");}
}
}
class JavaStudy_jButton1_actionAdapter implements java.awt.event.ActionListener {
JavaStudy adaptee; JavaStudy_jButton1_actionAdapter(JavaStudy adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
/**
* @author wukai
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JavaStudy extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JLabel jLabel1 = new JLabel();
JPanel jPanel1 = new JPanel();
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
GridLayout gridLayout1 = new GridLayout();
JPanel jPanel2 = new JPanel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
GridLayout gridLayout2 = new GridLayout();
JButton jButton1 = new JButton();
public JavaStudy(String s){
super();
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public void jbInit(){
this.getContentPane().setLayout(borderLayout1);
jLabel1.setBorder(BorderFactory.createEtchedBorder());
jLabel1.setToolTipText("");
jLabel1.setDisplayedMnemonic('0');
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("程序事例");
jPanel1.setLayout(gridLayout1);
gridLayout1.setColumns(0);
gridLayout1.setHgap(0);
gridLayout1.setRows(2);
gridLayout1.setVgap(10);
jLabel2.setText("输入");
jLabel3.setText("输出");
jPanel2.setLayout(gridLayout2);
gridLayout2.setRows(2);
gridLayout2.setVgap(10);
jButton1.setText("确定");
jButton1.addActionListener(new JavaStudy_jButton1_actionAdapter(this));
jTextField1.setText("");
jTextField2.setText("");
jPanel1.add(jTextField1, null);
jPanel1.add(jTextField2, null);
this.getContentPane().add(jPanel2, BorderLayout.WEST);
jPanel2.add(jLabel2, null);
jPanel2.add(jLabel3, null);
this.getContentPane().add(jButton1, BorderLayout.SOUTH);
this.getContentPane().add(jLabel1, BorderLayout.NORTH);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
}
public static void main(String[] args) {
JavaStudy javastudy=new JavaStudy("小程序");
javastudy.show();
} void jButton1_actionPerformed(ActionEvent e) {
String b,a;
b="boy";
a="girl";
if (jTextField1.getText().equals(b))
{ jTextField2.setText("男孩");}
if (jTextField1.getText().equals(a) )
{ jTextField2.setText("女孩");}
else
{ jTextField2.setText("错误,无此词");}
}
}
class JavaStudy_jButton1_actionAdapter implements java.awt.event.ActionListener {
JavaStudy adaptee; JavaStudy_jButton1_actionAdapter(JavaStudy adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
@author metaphy
@memo a colck
@2004-11-7
*/
import java.util.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.Timer;
public class MyClock {
public static void main(String[] args) {
MyClockFrame frame = new MyClockFrame();
frame.setDefaultCloseOperation(3);
frame.show();
}
}class MyClockFrame extends JFrame {
public MyClockFrame(){
setTitle ("a clock");
setResizable(false);
setSize(WIDTH,HEIGHT);
MyClockPanel panel = new MyClockPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
private int WIDTH = 400;
private int HEIGHT = 300;
}class MyClockPanel extends JPanel {
public MyClockPanel(){
ActionListener listener = new MyClockListener();
Timer t = new Timer(500,listener);
t.start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
// DRAW THE CLOCK
Ellipse2D circle = new Ellipse2D.Double(POINT_X,POINT_Y,2*RADIUS,2*RADIUS);
g2.draw(circle);
Line2D line = new Line2D.Double(POINT_X+RADIUS,POINT_Y,POINT_X+RADIUS,POINT_Y + LEN);
g2.draw(line);
line = new Line2D.Double(POINT_X+2*RADIUS,POINT_Y+RADIUS,POINT_X+2*RADIUS-LEN,POINT_Y + RADIUS);
g2.draw(line);
line = new Line2D.Double(POINT_X+RADIUS,POINT_Y+2*RADIUS,POINT_X+RADIUS,POINT_Y + 2*RADIUS-LEN);
g2.draw(line);
line = new Line2D.Double(POINT_X,POINT_Y+RADIUS,POINT_X+LEN,POINT_Y + RADIUS);
g2.draw(line);
g2.draw(hand1);
g2.draw(hand2);
g2.draw(hand3);
} public void setHands(int h,int m,int s){
Point2D end ;
double secondAngle = s/30.0*Math.PI ;
end = new Point2D.Double(POINT_X + RADIUS + SECOND_HAND_LEN * Math.sin(secondAngle), POINT_Y + RADIUS - SECOND_HAND_LEN * Math.cos(secondAngle));
hand3 = new Line2D.Double(center,end); //second hand double minuteAngle = m/30.0*Math.PI ;
end = new Point2D.Double(POINT_X + RADIUS + MINUTE_HAND_LEN * Math.sin(minuteAngle), POINT_Y + RADIUS - MINUTE_HAND_LEN * Math.cos(minuteAngle));
hand2 = new Line2D.Double(center,end); //minute hand double hourAngle = ((h%12)*60.0+m)/(6*60.0) * Math.PI ;
end = new Point2D.Double(POINT_X + RADIUS + HOUR_HAND_LEN * Math.sin(hourAngle), POINT_Y + RADIUS - HOUR_HAND_LEN * Math.cos(hourAngle));
hand1 = new Line2D.Double(center,end); //hour hand
}
public void setSecondMinuteHour(){
String strNow = new Date().toString();
String[] str = strNow.split(":");
hour = Integer.parseInt(str[0].substring(str[0].length()-2));
minute = Integer.parseInt(str[1]);
second = Integer.parseInt(str[2].substring(0,2));
} private double RADIUS = 100 ;
private int FRAME_WIDTH = 400;
private int FRAME_HEIGHT = 300;
private double POINT_X =(FRAME_WIDTH-2*RADIUS)/2;
private double POINT_Y =(FRAME_HEIGHT-2*RADIUS)/4;
private Point2D center = new Point2D.Double(POINT_X + RADIUS,POINT_Y + RADIUS);
private double SECOND_HAND_LEN = 0.85*RADIUS;
private double MINUTE_HAND_LEN = 0.70*RADIUS;
private double HOUR_HAND_LEN = 0.55*RADIUS;
private double LEN = 5 ;
private int second,minute,hour;
private Line2D hand1 =new Line2D.Double();
private Line2D hand2 =new Line2D.Double();
private Line2D hand3 =new Line2D.Double(); private class MyClockListener implements ActionListener{
public void actionPerformed(ActionEvent evt){
setSecondMinuteHour();
System.out.println(hour+":"+minute+":"+second);
setHands(hour,minute,second);
repaint();
}
}
}
void jButton1_actionPerformed(ActionEvent e) {
String b,a;
b="boy";
a="girl";
//下面这里这样就行了if-else if-else有问题,
if (jTextField1.getText().equals(b))
{ jTextField2.setText("男孩");}
else
if (jTextField1.getText().equals(a) )
{ jTextField2.setText("女孩");}
else
{ jTextField2.setText("错误,无此词");}
}
//...
Struts 配置
*/
1、新建WEB应用(WEB-INF/lib 和WEB-INF/tld);
2、将Struts中的lib/commons-*.jar 和 lib/struts.jar 拷贝到应用中WEB-INF/lib
下,将标签描述文件lib/struts-*.tld 拷贝到应用WEB-INF/tld 下;
3、在WEB-INF下建立 web.xml 文件,并加入以下内容,
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<display-name>Struts Blank Application</display-name>
<!--Standard Action Servlet Configuration(with debugging)-->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>ApplicationResources</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!--Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!--The Usual Weblcome File List-->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!--Struts Tag Library Descriptions-->
<taglib>
<taglib-uri>/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-html</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-template</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-template.tld</taglib-location>
</taglib>
</web-app>4、修改完web.xml,在WEB-INF目录创建一个名字为 struts-config.xml 的文件。
并添加以下初始化内容。
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts-config PUBLIC
PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
</form-beans>
<global-forwards>
</global-forwards>
<action-mappings>
</action-mappings>
</struts-config>5、测试,在WEB-INF/classes 目录建立一个ApplicationResources.properties 文件,增加一个
属性: index.title = Struts Tutorial
建立一个test.jsp<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib uri="/struts-logic" prefix="logic" %>
<%@ taglib uri="/struts-bean" prefix="bean" %>
<%@ taglib uri="/struts-html" prefix="html" %><html:html locale="true">
<head>
<html:base/>
<title>
<bean:message key = "index.title" />
</title>
</head>
<body>
This is a Struts Framework Program!
</html:html>
//OK!搞定!
//classpath 的一个问题
CLASSPATH 设置中有这么一个 d:\j\corejava\classes;.;....
在d:\j\corejava\classes\com\下面有两个类//A.java
package com ;
public class A{
int i = -1;
public int getI(){
return i;
}
}
//B.java
package com ;
public class B{
private A a = new A();
public int f(){
int b= a.getI();
return b ;
}
}
编译通过!
参考《JSP应用开发详解》电子工业出版社
PART I/III/**
SQL Server 2000
Tomcat 4.1
Struts jakarta-struts-1.1
Editplus
*///----数据库脚本----
create database dba
create table Tuser(uname varchar(64),upassword varchar(64),uage int)//----Tomcat 配置----
<Context path="/struts" docBase="E:\struts" debug="0"
reloadable="true" crossContext="true">
</Context>//----CLASSPATH 设置----
//由于我用的是基本文本编辑器,所以struts的jar 文件需要手工配置到CLASSPATH中
.;E:\struts\WEB-INF\classes;E:\struts\WEB-INF\lib;D:\WebSet\jdk\lib\dt.jar;D:\WebSet\jdk\lib\tools.jar;D:\WebSet\jdk\lib\msbase.jar;D:\WebSet\jdk\lib\mssqlserver.jar;D:\WebSet\jdk\lib\msutil.jar;D:\WebSet\jdk\lib\iTextAsian.jar;D:\WebSet\jdk\lib\jxl.jar;E:\struts\WEB-INF\lib\struts.jar;E:\struts\WEB-INF\lib\commons-beanutils.jar;E:\struts\WEB-INF\lib\commons-collections.jar;E:\struts\WEB-INF\lib\commons-digester.jar;E:\struts\WEB-INF\lib\commons-fileupload.jar;E:\struts\WEB-INF\lib\commons-lang.jar;E:\struts\WEB-INF\lib\commons-logging.jar;E:\struts\WEB-INF\lib\commons-validator.jar//----Struts 配置-----
E:\struts\WEB-INF\lib 的目录
2003-06-29 118,726 commons-beanutils.jar
2003-06-29 165,119 commons-collections.jar
2003-06-29 109,096 commons-digester.jar
2003-06-29 22,379 commons-fileupload.jar
2003-06-29 63,980 commons-lang.jar
2003-06-29 31,605 commons-logging.jar
2003-06-29 46,865 commons-validator.jar
2003-06-29 498,051 struts.jar
【2002-05-03 302,282 msbase.jar】
【2002-05-03 69,477 mssqlserver.jar】
【2002-05-03 67,235 msutil.jar】E:\struts\WEB-INF\tld 的目录
2003-06-29 8,868 struts-bean.tld
2003-06-29 66,192 struts-html.tld
2003-06-29 14,511 struts-logic.tld
2003-06-29 64,659 struts-nested.tld
2003-06-29 1,631 struts-template.tld
2003-06-29 7,850 struts-tiles.tld//---- WEB-INF\web.xml ----
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<display-name>Struts Blank Application</display-name>
<!--Standard Action Servlet Configuration(with debugging)-->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>ApplicationResources</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!--Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!--The Usual Weblcome File List-->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!--Struts Tag Library Descriptions-->
<taglib>
<taglib-uri>/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-html</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-tiles.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-template</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-template.tld</taglib-location>
</taglib>
</web-app>//---- WEB-INF\struts-config.xml ----
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name = "userForm" type="com.stru.UserForm" />
</form-beans>
<global-forwards>
<forward name = "userCreated" path="/viewUser.jsp" />
</global-forwards>
<action-mappings>
<action path = "/createUser"
type = "com.stru.UserAction"
name = "userForm"
scope= "request"
validate="true"
input ="/createUser.jsp">
</action>
</action-mappings>
<message-resources parameter = "ApplicationResources" />
</struts-config>//---- WEB-INF\ApplicationResources.properties ----
index.title = Struts Tutorial
//---- 类的编写 ----
E:\struts\WEB-INF\classes\com\stru 的目录
Dbcon.class //封装基本的数据库操作
User.class //基本的 getter,setter 方法
UserBean.class //实现具体的业务逻辑(通常是数据库的操作),被Action 类调用
UserAction.class //Action
UserForm.class //Form//---- Dbcon.java ----
package com.stru;
/**
基本的数据库操作类
*/
import java.sql.*;
import java.net.*;
import java.util.StringTokenizer;
public class Dbcon{
String driverStr="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbIp="127.0.0.1";
String dbName="dba";
String dbUser="sa";
String dbPwd="jnxr@))$";
String errTag="";
String dbUrl=""; public void setDbIp(String value){
dbIp=value;
}
public String getDbIp(){
return dbIp;
}
public void setDbName(String value){
dbName=value;
}
public String getDbName(){
return dbName;
}
public void setDbUser(String value){
dbUser=value;
}
public String getDbUser(){
return dbUser;
} public void setDbPwd(String value){
dbPwd=value;
}
public String getDbPwd(){
return dbPwd;
} public void setErrbz(String value){
errTag=value;
}
public String getErrbz(){
return errTag;
}
public boolean dbtry(){
try{
dbUrl="jdbc:microsoft:sqlserver://"+dbIp+":1433;DatabaseName="+dbName;
Class.forName(driverStr); //加载驱动
Connection conn= DriverManager.getConnection(dbUrl,dbUser,dbPwd); //加载数据库连接
return true;
}catch(Exception e){
return false;
}
} public String dbTest(){
try{
dbUrl="jdbc:microsoft:sqlserver://"+dbIp+":1433;DatabaseName="+dbName;
Class.forName(driverStr); //加载驱动
Connection conn= DriverManager.getConnection(dbUrl,dbUser,dbPwd); //加载数据库连接
return "Database Connection OK!";
}catch(Exception e){
return "Error:\n"+e.toString();
}
} public void dbexe(String sqlstr){
Connection conn=null;
Statement stmt=null;
try{
dbUrl="jdbc:microsoft:sqlserver://"+dbIp+":1433;DatabaseName="+dbName;
Class.forName(driverStr);
conn= DriverManager.getConnection(dbUrl,dbUser,dbPwd);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.execute(sqlstr);
errTag="ok";
}catch(Exception e){
System.out.println(e.toString());
System.out.println(sqlstr);
errTag="error";
}finally{
try{
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}catch(SQLException ee){
System.out.println(ee.toString());
}
}
} public ResultSet dbqry(String sqlstr){ //数据源未关闭
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try{
dbUrl="jdbc:microsoft:sqlserver://"+dbIp+":1433;DatabaseName="+dbName;
Class.forName(driverStr);
conn= DriverManager.getConnection(dbUrl,dbUser,dbPwd);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sqlstr);
errTag="ok";
}catch(Exception e){
System.out.println(e.toString());
System.out.println(sqlstr);
errTag="error";
}
return rs;
} public int qnum(String sqlstr){
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
int num=-1;
try{
dbUrl="jdbc:microsoft:sqlserver://"+dbIp+":1433;DatabaseName="+dbName;
Class.forName(driverStr);
conn= DriverManager.getConnection(dbUrl,dbUser,dbPwd);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sqlstr);
if(rs.next()){
num=rs.getInt(1);
errTag="ok";
}
}catch(Exception e){
System.out.println(e.toString());
System.out.println(sqlstr);
errTag="error";
}finally{
try{
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}catch(SQLException ee){
System.out.println(ee.toString());
}
}
return num;
}
}//---- User.java ----package com.stru;
/**
最基本的getter,setter 方法
*/
public class User {
public void setName(String aName){
this.name = aName;
}
public String getName(){
return this.name;
}
public void setPassword(String aPassword){
this.password = aPassword;
}
public String getPassword(){
return this.password ;
}
public void setAge(int aAge){
this.age = aAge;
}
public int getAge(){
return this.age ;
} public static void main(String[] args) {
System.out.println("Current User");
} private String name;
private String password;
private int age;
}
import java.sql.*;
import javax.sql.*;
import java.io.*;
/**
实现具体的业务逻辑(通常是数据库的操作),被Action 类调用;
但和HttpRequest 无关
*/
public class UserBean {
private Dbcon dbcon ; public UserBean () throws Exception{
dbcon = new Dbcon();
}
public void addUser(User user) throws SQLException{
String sql = "insert into Tuser(uname,upassword,uage) values('"+user.getName()+"','"+user.getPassword()+"',"+user.getAge()+")";
dbcon.dbexe(sql);
}
}//---- UserAction.java ----
package com.stru;
import javax.servlet.http.*;
import org.apache.struts.action.* ;
/**
ACTION
*/
public class UserAction extends Action {
public ActionForward perform(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
UserForm f =(UserForm)form;
try{
UserBean userBean = new UserBean();
userBean.addUser(f.getUser());
}
catch (Exception e){
e.printStackTrace();
}
request.setAttribute("User",f.getUser());//
return (mapping.findForward("userCreated"));
}
}//---- UserForm.java ----
package com.stru;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.* ;/**
FORM
*/
public class UserForm extends ActionForm {
public void setUserName(String aUserName){
user.setName(aUserName);
}
public String getUserName(){
return user.getName();
}
public void setUser(User user){
this.user = user ;
}
public User getUser(){
return this.user ;
}
/**
reset the form
*/
public void reset(ActionMapping mapping,HttpServletRequest request){
this.user = new User() ;
}
/**
check the form
*/
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request){
ActionErrors errors = new ActionErrors() ;
if (user.getName() == null || user.getName().trim().equals("") || user.getPassword().trim().equals("")){
errors.add("userInputError",new ActionError("errorInput:name/password"));
}
return errors ;
}
private User user = new User();
}//---- JSP 页面的编写 ----
2004-11-15 createUser.jsp //输入区域
2004-11-15 viewUser.jsp //执行返回//---- createUser.jsp -----
<%@ page contentType="text/html; charset=GB2312"%>
<%@ taglib uri="/struts-logic" prefix="logic" %>
<%@ taglib uri="/struts-bean" prefix="bean" %>
<%@ taglib uri="/struts-html" prefix="html" %>
<html:html locale ="true">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
<html:base/>
<title><bean:message key = "index.title" /></title>
</head>
<body>
<h2>创建一个新用户</h2>
<html:errors/>
<html:form action = "createUser.do" method="GET">
name:<html:text property = "user.name" /></br>
password:<html:password property = "user.password" /></br>
age :<html:text property = "user.age" /></br>
<html:submit property ="submit" />
</html:form>
</body>
</html:html>//----- viewUser.jsp ----
<%@ page contentType="text/html; charset=GB2312" import="com.stru.User"%>
<%@ taglib uri="/struts-logic" prefix="logic" %>
<%@ taglib uri="/struts-bean" prefix="bean" %>
<%@ taglib uri="/struts-html" prefix="html" %><html:html locale ="true">
<head>
<html:base/>
<title><bean:message key = "index.title" /></title>
</head><body>
<%User user = (User)request.getAttribute("User"); %>
已经创建了用户7<br>
Name:<%=user.getName()%><br>
Password:<%=user.getPassword()%><br>
Age:<%=user.getAge()%><br>
</body>
</html:html>//---- 最后测试 ----
http://localhost:8080/struts/createUser.jsp
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
顺便想问一下:为什么JAVAC hello.java 中的hello可以不大写而JAVA hello必须大字才行呢?
也就是说编译的时候文件名可以不区分大小写而解释的时候则需要区分大小写呢?(这也是我的HELLO WORLD程序用了两周才出来的原因)
简易的字符串替换,适合jdk1.3版本,因为jdk1.4自带
*/
public class StringReplace
{
public static void main(String[] args)
{
System.out.println("Hello World!");
String result = replaceAll("bbabcdefg","uu","88");
System.out.println(result);
}
public static String replaceAll(String old,String oldSub,String newSub){
int currpos = -1; //当前位置
int priPos = 0;//上一次位置
int oldlen = oldSub.length();//子字符串长度
String newString = "";
while((currpos = old.indexOf(oldSub,priPos)) != -1){
newString += old.substring(priPos,currpos);
newString += newSub;
priPos = currpos + oldlen;
}
newString += old.substring(priPos);
return newString;
}
}
懂JAVA的好心人加我QQ吧:41777139.谢谢!(楼主别生气啊.占你宝地了^_^)
简易的字符串替换*/
public class StringReplace
{
public static void main(String[] args)
{
System.out.println("Hello World!");
String result = replaceAll("bbabcdefg","uu","88");
System.out.println(result);
}
public static String replaceAll(String old,String oldSub,String newSub){
int currpos = -1; //当前位置
int priPos = 0;//上一次位置
int oldlen = oldSub.length();//子字符串长度
String newString = "";
while((currpos = old.indexOf(oldSub,priPos)) != -1){
newString += old.substring(priPos,currpos);
newString += newSub;
priPos = currpos + oldlen;
}
newString += old.substring(priPos);
return newString;
}
}
也来一篇:
import java.text.*;
//import javax.swing.*;public class CompoundInterest
{
public static void main(String[] args)
{
final int STARTATE=10;
final int NRATES=6;
final int NYEARS=10;
//-----------------------------
double[ ] interestRate=new double[NRATES];
for(int j=0;j<interestRate.length;j++)
interestRate[j]=(STARTATE+j)/100.0; double[ ][ ] balance=new double[NYEARS][NRATES];
//--------------
for(int j=0;j<balance[0].length;j++)
balance[0][j]=10000;
//--------------
for(int i=1;i<balance.length;i++)
{
for(int j=0;j<balance[i].length;j++)
{
double oldBalance=balance[i-1][j];
double interest=oldBalance*interestRate[j];
balance[i][j]=oldBalance+interest;
}
}
NumberFormat formatter=NumberFormat.getPercentInstance();
for (int j=0;j<interestRate.length;j++)
{
System.out.println(" ");
//System.out.println(interestRate[j]);
System.out.println(formatter.format(interestRate[j]));
}
System.out.println();
formatter=NumberFormat.getCurrencyInstance();
for(int i=0;i<balance.length;i++)
{
for(int j=0;j<balance[i].length;j++)
{
System.out.print(" ");
System.out.println(formatter.format(balance[i][j]));
}
System.out.println();
}
}
}