Joint user registration with beans and servlet in JSP

Source: Internet
Author: User
Tags html page insert mysql string stmt tostring mysql database stringbuffer
Js|servlet| user registers the software and operating environment required by this example:
1. Windows2000 Server operating system
2, jdk1.4
3, JCreator2.5 (Java source Edit debugger)
4, Macromedia JRun MX
5, Macromedia Dreamweaver MX (not required)
6, MySQL database (preferably installed MySQL control Center)

first, the database design
With MySQL control center open the MySQL database, create a new database shopping, under which the new table Tbl_user, where the fields are set as follows:

Second, write the connection database 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 Database driver
This.conn = drivermanager.getconnection (Sql_url + "/" + Sql_dbname + "? user=" + user + "&password=" + pwd + "&useu") nicode=true&characterencoding=gb2312 ");
this.stmt = This.conn.createStatement ();
}catch (Exception e) {
System.out.println (E.tostring ());
}
}

Perform query operations
Public ResultSet executequery (String strSQL)
{
try{
this.rs = Stmt.executequery (strSQL);
return this.rs;
}catch (SQLException e) {
System.out.println (E.tostring ());
return null;
}catch (NullPointerException e) {
System.out.println (E.tostring ());
return null;
}
}

Perform insert, delete, modify operation of data
Public Boolean execute (String strSQL)
{
try{
if (this.stmt.executeUpdate (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;
}
}

Result set pointer jumps 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 get a large segment of text.
Replace the carriage return line with the

Output to an 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 the STR2 string in the str1 string with the 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 if there is information 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, adjust the trial
public static void Main (String args[])
{
try{
Dbconn myconn = new Dbconn ();
Myconn.setdbname ("shopping");
MyConn. Dbconn ();
Myconn.execute ("Insert into Tbl_test (id,name) VALUES (' Ten ', ' Shandaer ')");
Myconn.execute ("Update tbl_test set name= ' yyyyyyyyyyyy ' where id=10");
Myconn.execute ("Delete from Tbl_test where id=1");
ResultSet rs = myconn.executequery ("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 ());
}
}

}

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.