The operation application of MySQL in JSP environment

Source: Internet
Author: User
Js|mysql

Premise:

Place the MySQL database driver in the Web-inf\lib directory of the working directory (so you can link to the JSP)

With a JavaBean connection, the compiled. class file is placed under the classes file, and if the file contains package instructions, it is placed in the

Under the specified directory.

At this point, there is no problem with the data query, but Update,delete and insert are not valid. (feasible in SQL Server)

Problem resolution, see JDK Description, Find statement method segment ResultSet executequery (string), int executeupdate (string)

Modify JavaBean, add Executeupdate method, modify. jsp file, point to Executeupdate when not select, test Update,insert,

Delete is successfully implemented

ExecuteQuery Method Code:

Public ResultSet executequery (String sqlstring)
{

Rs=null;
Try
{

Conn=drivermanager.getconnection (CONNURL,USERNAME,PWD);
Statement stmt=conn.createstatement ();
Rs=stmt.executequery (SqlString);
}
catch (SQLException ex)
{
System.err.println ("Aq.executequery:" +ex.getmessage ());
}

Return RS;
}

Excuteupdate Method Code:

public int executeupdate (String sqlstring)
{
instructioncount=0;
Try
{

Conn=drivermanager.getconnection (CONNURL,USERNAME,PWD);
Statement stmt=conn.createstatement ();
Stmt.executeupdate (SqlString);
Instructioncount=1;
}
catch (SQLException ex)
{
System.err.println ("Aq.executequery:" +ex.getmessage ());
}

return instructioncount;
}

New problem: When MySQL uses utf-8 to support all Chinese, the decoding of the payment string will destroy the Chinese input.

When inserting and updating data, cancel the original encoding with the GBK new String

Author

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.