Use Bean and Servlet in Jsp to implement user registration

Source: Internet
Author: User
Article title: using Bean and Servlet in Jsp to implement user registration. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
The software and operating environment required in this example:
1. Windows Server operating system
2. jdk1.4
3. JCreator2.5 (java source code editor debugger)
4. Macromedia JRun MX
5. Macromedia Dreamweaver MX (not required)
6. MySQL database (preferably MySQL Control Center)
  
   I. database design
Use MySQL Control Center to open the MySQL database, create a database shopping, and create a table tbl_user under it. The fields are set as follows:
  
   2. write a database connection bean: DBConn. java
// DBConn. java
  
// Include required classes
Import java. SQL .*;
  
// ================================================ ====
// Define Class DBConn
// ================================================ ====
Public class DBConn
{
Public String SQL _driver = "org. gjt. mm. mysql. Driver ";
Public String SQL _url = "jdbc: mysql: // localhost: 3306 ";
Public String SQL _DBName = "shopping ";
Public String user = "sa ";
Public String pwd = "";
  
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
  
Public boolean setDriver (String drv)
{
This. SQL _driver = drv;
Return true;
}
  
Public String getDriver ()
{
Return this. SQL _driver;
}
  
Public boolean setUrl (String url)
{
This. SQL _url = url;
Return true;
}
  
Public boolean setDBName (String dbname)
{
This. SQL _DBName = dbname;
Return true;
}
  
Public String getDBName ()
{
Return this. SQL _DBName;
}
  
Public boolean setUser (String user)
{
This. user = user;
Return true;
}
  
Public String getUser ()
{
Return this. user;
}
  
Public boolean setPwd (String pwd)
{
This. pwd = pwd;
Return true;
}
  
Public String getPwd ()
{
Return this. pwd;
}
  
Public DBConn ()
{
Try {
Class. forName (SQL _driver); // load the database driver
This. conn = DriverManager. getConnection (SQL _url + "/" + SQL _DBName + "? User = "+ user +" & password = "+ pwd +" & useUnicode = true & characterEncoding = gb2312 ");
This. stmt = this. conn. createStatement ();
} Catch (Exception e ){
System. out. println (e. toString ());
}
}
  
// Perform the query operation
Public ResultSet executeQuery (String strSql)
{
Try {
This. rs = stmt.exe cuteQuery (strSql );
Return this. rs;
} Catch (SQLException e ){
System. out. println (e. toString ());
Return null;
} Catch (NullPointerException e ){
System. out. println (e. toString ());
Return null;
}
}
  
// Insert, delete, and modify data
Public boolean execute (String strSql)
{
Try {
If(this.stmt.exe cuteUpdate (strSql) = 0)
Return false;
Else
Return true;
} Catch (SQLException e ){
System. out. println (e. toString ());
Return false;
} Catch (NullPointerException e ){
System. out. println (e. toString ());
Return false;
}
}
  
// Jump the result set pointer to a row
Public boolean rs_absolute (int row)
{
Try {
This. rs. absolute (row );
Return true;
} Catch (SQLException e ){
System. out. println (e. toString ());
Return false;
}
}
  
Public void rs_afterLast ()
{
Try {
This. rs. afterLast ();
} Catch (SQLException e ){
System. out. println (e. toString ());
}
}
  
Public void rs_beforeFirst ()
{
Try {
This. rs. beforeFirst ();
} Catch (SQLException e ){
System. out. print (e. toString ());
}
}
  
Public void rs_close ()
{
Try {
This. rs. close ();
} Catch (SQLException e ){
System. out. print (e. toString ());
}
}
  
Public void rs_deleteRow ()
{
Try {
This. rs. deleteRow ();
} Catch (SQLException e ){
System. out. print (e. toString ());
}
}
  
Public boolean rs_first ()
{
Try {
This. rs. first ();
Return true;
} Catch (SQLException e ){
System. out. print (e. toString ());
Return false;
}
}
  
Public String rs_getString (String column)
{
Try {
Return this. rs. getString (column );
} Catch (SQLException e ){
System. out. println (e. toString ());
Return null;
}
}
  
// This method is used to obtain large text segments,
// Replace the carriage return line

// Output to the html page
Public String rs_getHtmlString (String column)
{
Try {
String str1 = this. rs. getString (column );
String str2 = "\ r \ n ";
String str3 ="
";
Return this. replaceAll (str1, str2, str3 );
} Catch (SQLException e ){
System. out. println (e. toString ());
Return null;
}
}
  
// Replace str2 string in str1 with str3 string
Private static String replaceAll (String str1, String str2, String str3)
{
StringBuffer strBuf = new StringBuffer (str1 );
Int index = 0;
While (str1.indexOf (str2, index )! =-1)
{
Index = str1.indexOf (str2, index );
StrBuf. replace (str1.indexOf (str2, index), str1.indexOf (str2, index) + str2.length (), str3 );
Index = index + str3.length ();
  
Str1 = strBuf. toString ();
}
Return strBuf. toString ();
}
  
Public int rs_getInt (String column)
{
Try {
Return this. rs. getInt (column );
} Catch (SQLException e ){
System. out. println (e. toString ());
Return-1;
}
}
  
Public int rs_getInt (int column)
{
Try {
Return this. rs. getInt (column );
} Catch (SQLException e ){
System. out. println (e. toString ());
Return-1;
}
}
  
Public boolean rs_next ()
{
Try {
Return this. rs. next ();
} Catch (SQLException e ){
System. out. println (e. toString ());
Return false;
}
}
  
// Determine whether there is data in the result set
Public boolean hasData ()
{
Try {
Boolean has_Data = this. rs. first ();
This. rs. beforeFirst ();
Return has_Data;
} Catch (SQLException e ){
System. out. println (e. toString ());
Return false;
}
}
  
Public boolean rs_last ()
{
Try {
Return this. rs. last ();
} Catch (SQLException e ){
System. out. println (e. toString ());
Return false;
}
}
  
Public boolean rs_previous ()
{
Try {
Return this. rs. previous ();
} Catch (Exception e ){
System. out. println (e. toString ());
Return false;
}
}
  
// Main method for debugging
Public static void main (String args [])
{
Try {
DBConn myconn = new DBConn ();
// Myconn. setDBName ("shopping ");
// Myconn. DBConn ();
// Myconn.exe cute ("Insert Into tbl_test (id, name) values ('10', 'shandaer ')");
// Myconn.exe cute ("Update tbl_test set name = 'yyyyyyyyyy' where id = 10 ");
// Myconn.exe cute ("Delete from tbl_test where id = 1 ");
ResultSet rs = myconn.exe cuteQuery ("select * from tbl_user order by id desc limit 1 ");
// Boolean hasData = myconn. hasData ();
// System. out. println ("has data:" + hasData );
// Rs. first ();
While (myconn. rs. next ())
{
Int id = myconn. rs_getInt ("id") + 1;
System. out. print (id );
System. out. println (myconn. rs_getInt ("id") + myconn. rs_getString ("name "));
  
// System. out. println ('\ n' + myconn. rs_getHtmlString ("name "));
// System. out. println (myconn. rs. getString ("name") + myconn. rs_getInt (1 ));
}
} Catch (Exception e ){
System. err. println (e. toString (

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.