Database Connection operations (1)

Source: Internet
Author: User
Connect to the database using pure JSP, including data query, deletion, modification, and addition of data in the database. The database contains login for MySQL files. jsp, do_login.jsp (processing login information), index. jsp (display logon username, password, and edit delete operation), edit. jsp, do_edit.jsp, delete. jsp login page login. jspvc

Connect to the database using pure JSP, including data query, deletion, modification, and addition of data in the database. The database contains login for MySQL files. jsp, do_login.jsp (processing login information), index. jsp (display logon username, password, and edit delete operation), edit. jsp, do_edit.jsp, delete. jsp login page login. jsp vc

Connect to the Database Using JSP, including data query, deletion, and modification. add data to the database. The database is MySQL.

The files include login. jsp, do_login.jsp (process logon information), index. jsp (display logon username, password, and edit and delete operations), edit. jsp, do_edit.jsp, and delete. jsp.

Logon page


Login. jsp <喎?http: www.2cto.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PHByZSBjbGFzcz0 = "brush: SQL;"> <% @ page language = "java" pageEncoding = "UTF-8" %> Logon page <% Request. setCharacterEncoding ("gb2312"); response. setContentType ("text/html; charset = gb2312"); %>

Welcome to register this website Do_login.jsp

<% @ Page language = "java" import = "java. SQL. *" pageEncoding = "UTF-8" %>        Logon processing page     <% Request. setCharacterEncoding ("gb2312"); response. setContentType ("text/html; charset = gb2312"); String user = request. getParameter ("username"); String pwd = request. getParameter ("password"); final String DBDriver = "com. mysql. jdbc. driver "; final String DBUrl =" jdbc: mysql: // localhost: 3306/mqsqltest "; final String DBUser =" root "; final String DBPassword =" 686175 "; connection conn = null; Statement stmt = null; Re SultSet rs = null; Class. forName (DBDriver); conn = DriverManager. getConnection (DBUrl, DBUser, DBPassword); String SQL = "select * from user where username = '" + user + "' & password = '" + pwd + "'"; stmt = conn. createStatement (); rs1_stmt.exe cuteQuery (SQL); if (rs. next () {out. print ("Logon successful! "); Response. setHeader (" refresh "," 2; url = index. jsp ");} else {out. println (" Your Logon account or password is incorrect! "); Response. setHeader (" refresh "," 2; url = login. jsp ");} stmt. close (); conn. close (); %>

Index. jsp

<% @ Page language = "java" import = "java. SQL. *" pageEncoding = "UTF-8" %>      Data list    <% Request. setCharacterEncoding ("gb2312"); response. setContentType ("text/html; charset = gb2312"); final String DBDriver = "com. mysql. jdbc. driver "; final String DBUrl =" jdbc: mysql: // localhost: 3306/mqsqltest "; final String DBUser =" root "; final String DBPassword =" 686175 "; connection conn = null; Statement stmt = null; ResultSet rs = null; Class. forName (DBDriver); conn = DriverManager. getConnection (DBUrl, DBUser, DBPassword); String SQL = "select * from user"; stmt = conn. createStatement (); rs1_stmt.exe cuteQuery (SQL); %>
 
 <% While (rs. next () {%> 
   <%} Stmt. close (); conn. close (); %> 
  
No. Account Password Edit Delete
<% = Rs. getInt (1) %> <% = Rs. getString (2) %> <% = Rs. getString (3) %> "> Update "> Delete
Edit page

After editing


Edit. jsp

<% @ Page language = "java" import = "java. SQL. *" pageEncoding = "UTF-8" %>      Edit page      <% Request. setCharacterEncoding ("gb2312"); response. setContentType ("text/html; charset = gb2312"); int id = Integer. parseInt (request. getParameter ("id"); session. setAttribute ("id", id); final String DBDriver = "com. mysql. jdbc. driver "; final String DBUrl =" jdbc: mysql: // localhost: 3306/mqsqltest "; final String DBUser =" root "; final String DBPassword =" 686175 "; connection conn = null; Statement stmt = null; ResultSet rs = null; Class. forName (DBDriver); conn = DriverManager. getConnection (DBUrl, DBUser, DBPassword); String SQL = "select * from user where id = '" + id + "'"; stmt = conn. createStatement (); rs1_stmt.exe cuteQuery (SQL); rs. next (); %>
 
  
Edit page
   
Process Edit page

Do_edit.jsp

<% @ Page language = "java" import = "java. SQL. *" pageEncoding = "UTF-8" %> <% @ page import = "java. util. *" %>        Edit save page     <% Request. setCharacterEncoding ("gb2312"); response. setContentType ("text/html; charset = gb2312"); int id = Integer. parseInt (session. getAttribute ("id "). toString (); String user = request. getParameter ("username"); String pwd = request. getParameter ("password"); final String DBDriver = "com. mysql. jdbc. driver "; final String DBUrl =" jdbc: mysql: // localhost: 3306/mqsqltest "; final String DBUser =" root "; final String DB Password = "686175"; Connection conn = null; Statement stmt = null; ResultSet rs = null; Class. forName (DBDriver); conn = DriverManager. getConnection (DBUrl, DBUser, DBPassword); stmt = conn. createStatement (); if (user! = Null & pwd! = Null) {String SQL = "update user set username = '" + user + "', password = '"+ pwd +" 'where id =' "+ id +" '"; stmt.exe cuteUpdate (SQL); out. print ("modified successfully! "); Response. setHeader (" refresh "," 2; url = index. jsp ");} else {out. print (" the account or password cannot be blank! "); Response. setHeader (" refresh "," 2; url = edit. jsp ") ;}%>
Delete page

Delete. jsp

<% @ Page language = "java" import = "java. SQL. *" pageEncoding = "UTF-8" %>       Delete page     <% Request. setCharacterEncoding ("gb2312"); response. setContentType ("text/html; charset = gb2312"); int id = Integer. parseInt (request. getParameter ("id"); final String DBDriver = "com. mysql. jdbc. driver "; final String DBUrl =" jdbc: mysql: // localhost: 3306/mqsqltest "; final String DBUser =" root "; final String DBPassword =" 686175 "; connection conn = null; Statement stmt = null; Class. forName (DBDriver); conn = DriverMana Ger. getConnection (DBUrl, DBUser, DBPassword); String SQL = "delete from user where id = '" + id + "'"; stmt = conn. createStatement (); stmt.exe cuteUpdate (SQL); stmt. close (); conn. close (); out. print ("deleted successfully! "); Response. setHeader (" refresh "," 2; url = index. jsp "); %>
Before compiling and running, import the MySQL driver package to WebRoot/WEB-INF/lib.

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.