Jsp+javabean step-by-Step Tutorial (v)

Source: Internet
Author: User
Tags error handling getmessage
Adduser.java file

Note: The main user data read and add operations, derived from the DB, Addnewuser method used to add user data, CheckUser () method to check the user name is duplicated, there are some set/get methods used to deal with the property, The dousernew.jsp file will be used in strips.

Copyright (c) http://jspbbs.yeah.net
Package Lyf;
/**
* A class class.
*


* @author Liuyufeng
*/
Importing Java class libraries
Import java.sql.*;
Import java.lang.*;
Import oracle.jdbc.driver.*;
AddUser is derived from DB and has the member variables and methods of DB
public class AddUser extends db {
Builder
public Boolean addnewuser () {
Boolean boadduser=false;
try {
Create a user-registered record add operation to generate an SQL statement
String Ssql=new string (insert into User (regtime,username,password,email,homepage,signs));
ssql=ssql+ "VALUES (Sysdaye," "+username+") "," "+password+" "," "+email+" "," "+homepage+" "," "+signs+" "" ";
A method of debugging that can print out SQL statements to facilitate viewing errors
System.out.println (sSQL);
Invokes the Executeupdate method of the parent class and sets the return value based on success
if (Super.executeupdate (sSQL)) boadduser=true;
}
catch (Exception ex) {

Error handling
System.err.println ("Adduser.addnewuser:" + ex.getmessage ());
}finally{
return value regardless of error
return boadduser;
}
}

CheckUser () method to check whether the user name is duplicated

If you repeatedly return a false
public Boolean checkUser () {
Boolean boadduser=false;
try {
Building SQL query Statements
String ssql= "SELECT * from user where username=" "+use me+" "";
Invoke the ExecuteQuery method of the parent class
if ((Super.executequery (sSQL)). Next ()) {
Query out Recordset is empty
Boadduser=false;
}else{
Boadduser=true;
}
}
catch (Exception ex) {
Error handling
System.err.println ("Adduser.addnewuser:" + ex.getmessage ());
}finally{
return value
return boadduser;
}
}


The Set/get method of the property, consistent with the requested parameter
/*
In fact, all the following get/set methods are repetitive labor, in order to avoid duplication of copy and paste work, I wrote a software JSP Code faster, as long as the input a series of field names, all the Get/set method can be automatically generated, you can in my website http:// jspbbs.yeah.net Download the Software
*/
Get/set method for username of attribute user name
Public String GetUserName () {
return Username;}
public void Setusername (String newusername) {
User name may be in Chinese and need to be converted
Username =db.tochinese (newusername);}
Get/set method of attribute password password
Public String GetPassword () {
return Password;}
public void SetPassword (String newpassword) {
Password = NewPassword;}
Get/set Method of attribute email
Public String Getemail () {
return Email;}
public void Setemail (String newemail) {
Email = Newemail;}
Properties Home Page Homepage Get/set method
Public String Gethomepage () {
return homepage;}
public void setHomePage (String newhomepage) {
Homepage = newhomepage;}

Properties Home Page Signs Get/set method
Public String getsigns () {
return signs;}
public void setsigns (String newsigns) {
The signature may be Chinese and needs to be converted
Signs = Db.tochinese (newsigns);}
}
OK, here, JavaBean program basically finished, should pay attention to the Chinese processing must be converted, and not necessarily all the attributes need to Set/get method, depending on the situation, and finally need to compile the class file, You can use some visual software, such as JBuilder or VisualAge, to compile. After compiling, you will find that there are two files Db.class and Adduser.class files, all in the LYF subdirectory. These two files will allow the following JSP pages to be invoked.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.