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