Editing an Oracle database using Java code

Source: Internet
Author: User

 PackageCom.hanqi;Importjava.io.IOException;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.*;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classSaveuserextendsHttpServlet {Private Static Final LongSerialversionuid = 1L;     PublicSaveuser () {Super();    }    protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {Request.setcharacterencoding ("UTF-8");//Set character setsResponse.setcontenttype ("text/html; Charset=utf-8 "); String strUserID= Request.getparameter ("userid");//get the arguments passed overString strUserName = Request.getparameter ("username"); String STRPW= Request.getparameter ("Password"); if(strUserID = =NULL|| Struserid.trim (). Length () ==0) {//determines whether the obtained parameter is emptyResponse.getwriter (). Append ("User ID cannot be empty"); }        Else if(strUserName = =NULL|| Strusername.trim (). Length () ==0) {Response.getwriter (). Append ("User name cannot be empty"); }        Else {            Try{class.forname ("Oracle.jdbc.driver.OracleDriver");//Registering OracleString url = "Jdbc:oracle:thin: @localhost: 1521:ORCL";//setting up a connection to an Oracle databaseConnection conn = drivermanager.getconnection (URL, "test1204", "1234");//Log in to Oracle, connect Oracle users                    if(conn!=NULL) {                        //manipulating DatabasesString sql = "INSERT into T_test (user_id, user_name, password)" + "VALUES (?,?,?)";//commands for setting Oracle statements//class that executes the statementPreparedStatement PS = conn.preparestatement (SQL);//create a Class PS and release the Execute commandstrUserName =NewString (Strusername.getbytes ("iso-8859-1"), "Utf-8");//transcodingPs.setstring (1, strUserID);//setting values to be written to the Oracle tablePs.setstring (2, strUserName); Ps.setstring (3, STRPW); introw = Ps.executeupdate ();//executes the statement's command and returns the number of rows of data                                                if(row>0) {Response.getwriter (). Append ("Save the data successfully, save the" +row+ "bar data successfully!"); }                        Else{response.getwriter (). Append ("Save data Failed");                        } ps.close ();                    Conn.close (); }                    Else{response.getwriter (). Append ("Failed to connect to database"); }                           } Catch(Exception e) {e.printstacktrace ();            Response.getwriter (). Append (E.getmessage ()); }        }    }    protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response); }}
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 strict//en" "Http://www.w3.org/TR/html4/strict.dtd" >user code:<input id= "userid" type= "text" name= "userid" width=30/><br> <br> user name:<input id= "username" type= "text" name= "username" width=30/><br><br>  Login Password:<input id= "password" type= "password" name= "password" width=30/><br><br> Confirm Password: <input id= "Queren" type= "password" name= "Queren" width=30/><br><br><input type= "Submit" value= " Submit "/></form></body>

Editing an Oracle database using Java code

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.