Summary of common jsp + mysql database operations examples, jspmysql

Source: Internet
Author: User

Summary of common jsp + mysql database operations examples, jspmysql

This document describes common methods for operating jsp + mysql databases. Share it with you for your reference. The details are as follows:

1. View:

<% @ Page contentType = "text/html; charset = GB2312" %> <% @ page import = "java. SQL. * "%> <HTML> <style type =" text/css "> <! -- Body {background-color: #99 CCFF;} --> </style> <BODY> <font color = "# FFFFFF"> <center> <% Connection con; Class. forName ("com. mysql. jdbc. driver "); con = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/student", "root", "123456"); Statement SQL; ResultSet rs; try {SQL = con. createStatement (); rs= SQL .exe cuteQuery ("SELECT * FROM student"); out. print ("<Table Border style = 'font-size: 10pt '>"); out. print ("<TR> <td colspan = 5 align = center> candidate data </td> </tr>"); out. print ("<TR> <td colspan = 5 align = center> <a href = 'add. jsp 'target = '_ self'> Add candidate information </a> </td> </tr> "); out. print ("<TR>"); out. print ("<Td width = 50>" + "name"); out. print ("<Td width = 100>" + "Age"); out. print ("<Td width = 100>" + "Date of Birth"); out. print ("<Td width = 100 colspan = 2>" + "operation"); out. print ("</TR>"); while (rs. next () {out. print ("<TR>"); out. print ("<TD>" + rs. getS Tring (2) + "</TD>"); out. print ("<TD>" + rs. getString (3) + "</TD>"); out. print ("<TD>" + rs. getString (4) + "</TD>"); String idstr = rs. getString (1); out. print ("<TD> <a href = 'delete. jsp? Id = "+ idstr +" '> Delete </a> </TD> "); out. print (" <TD> <a href = 'Update. jsp? Id = "+ idstr +" '> modify </a> </TD> "); out. print ("</TR>");} out. print ("</Table>"); con. close ();} catch (SQLException e1) {out. print ("SQL Exception !!!! ") ;}%> </Center> </BODY> </HTML>

2. add:

<% @ Page contentType = "text/html; charset = gb2312" %> <HTML> <HEAD> <style type = "text/css"> <! -- Body {background-image: url (); background-color: # CCCCFF ;}. STYLE5 {font-family: "Courier New", Courier, monospace; font-size: 14px ;}. STYLE6 {font-family: "Courier New", Courier, monospace; font-size: 24px ;} --> </style> <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312 "> </HEAD> <BODY> <Font size = 2> <p align =" center "class =" STYLE6 "> Add candidate information </p> <CENTER> <FORM action = "insert. jsp "name = form> <table> <tr> <td height =" 36 "> <span class =" STYLE5 "> name: </span> </td> <Input name = "name" type = text size = "15"> </td> </tr> <td height = "36"> <span class = "STYLE5"> age: </span> </td> <Input name = "age" type = text size = "15"> </td> </tr> <td height = "36"> <span class = "STYLE5"> Date of Birth: </span> </td> <Input name = "birth" type = text size = "15"> </td> </tr> </table> <table width = "165"> <tr> <td width = "42" wnameth = "42"> <Input type = submit name = "g" value = "add"> </td> <td width = "28" wnameth = "50"> </td> <td width = "42" wnameth = "50"> <Input type = "reset" name = "h" value = "reset"> </td> <td width = "33" wnameth = "42"> </td> </tr> </table> </Form> </CENTER> </Body> </HTML>

3. delete:

<% @ Page contentType = "text/html; charset = gb2312" %> <% @ page import = "java. SQL. * "%> 

4. update Example 1:

<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <Html xmlns =" http://www.w3.org/1999/xhtml "> <Head> <meta http-equiv =" Content-Type "content =" text/html; charset = gb2312 "/> <title> untitled document </title> <style type =" text/css "> <! -- Body {background-color: # FFCCFF;} --> </style> 

5. update Example 2:

<% @ Page contentType = "text/html; charset = gb2312" %> <% @ page import = "java. SQL. * "%> <% String name = request. getParameter ("name"); String age = request. getParameter ("age"); String birth = request. getParameter ("birth"); String id1 = request. getParameter ("id1"); System. out. println (id1); Connection con = null; try {Class. forName ("com. mysql. jdbc. driver "); con = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/stud Ent "," root "," 123456 "); Statement SQL; SQL = con. createStatement (); String sql2 = "update student set name = '" + name + "', age = '" + age + "', birth = '"+ birth +" 'where id = "+ id1; System. out. print (sql2); int s= SQL .exe cuteUpdate (sql2);} catch (Exception e) {System. out. println (e) ;}%> congratulations, modification successful! <Br/> <a href = "chakan. jsp"> View </a>

6. insert

<% @ Page contentType = "text/html; charset = gb2312" %> <% @ page import = "java. SQL. * "%> <% String name = request. getParameter ("name"); String age = request. getParameter ("age"); String birth = request. getParameter ("birth"); Connection con = null; try {Class. forName ("com. mysql. jdbc. driver "); con = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/student", "root", "123456"); Statement SQL; SQL = con. createStatement (); String sql2 = "insert into student (name, age, birth) values ('" + name + "', '" + age + "', '" + birth + "') "; System. out. print (sql2); int s= SQL .exe cuteUpdate (sql2);} catch (Exception e) {System. out. println (e) ;}%> congratulations! added successfully! <Br/> <a href = "chakan. jsp"> View </a>

7. Create a database

/*MySQL Data TransferSource Host: localhostSource Database: studentTarget Host: localhostTarget Database: studentDate: 2009-3-27 13:24:01*/SET FOREIGN_KEY_CHECKS=0;create database student;use student;-- ------------------------------ Table structure for student-- ----------------------------CREATE TABLE `student` ( `id` int(11) NOT NULL auto_increment, `name` varchar(255) default NULL, `age` varchar(255) default NULL, `birth` varchar(255) default NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=gbk;

I hope this article will help you with JSP program design.

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.