JSP Connection Database

Source: Internet
Author: User

JSP Connection database:

<%@ page language= "java" import= "java.util.*, java.sql.*" pageencoding= "GB18030"%>

<%

String username = request.getparameter ("username");

String Password = request.getparameter ("password");

String Password2 = Request.getparameter ("Password2");

Class.forName ("Com.mysql.jdbc.Driver");

Connection conn = drivermanager.getconnection ("Jdbc:mysql://localhost/bishe", "root", "admin");

String sqlquery = "SELECT count (*) from user where username =?";

PreparedStatement psquery = conn.preparestatement (sqlquery);

Psquery.setstring (1, username);

ResultSet rs = Psquery.executequery ();

Rs.next ();

int count = Rs.getint (1);

if (Count > 0) {

Response.sendredirect ("registerfail.jsp");

Psquery.close ();

Conn.close ();

Return

}

String sql = "INSERT into user values (null,?,?)";

PreparedStatement PS = conn.preparestatement (SQL);

Ps.setstring (1, username);

Ps.setstring (2, password);

Ps.executeupdate ();

Ps.close ();

Conn.close ();

Response.sendredirect ("registersuccess.jsp");

%>

JavaBean Database connection:

Class.forName ("Com.mysql.jdbc.Driver");

Connection conn = drivermanager.getconnection ("Jdbc:mysql://localhost/spring", "root", "BJSXT");

String sql = "INSERT into user values (null,?,?)";

PreparedStatement PS = conn.preparestatement (SQL);

Ps.setstring (1, U.getusername ());

Ps.setstring (2, U.getpassword ());

Ps.executeupdate ();

Ps.close ();

Conn.close ();

Hibernate data manipulation

Configuration cfg = new configuration (). Configure ();

Factory = Cfg.buildsessionfactory (). Opensession ();

Session session = Hibernateutil.getsession ();

Open transaction

Session.begintransaction ();

Save data

Session.save (P);

Transaction commit

Session.gettransaction (). commit ();

//Off Session

Hibernateutil.closesession (session);


This article is from the "sunnyting" blog, make sure to keep this source http://sunnyting.blog.51cto.com/8814143/1559589

JSP Connection Database

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.