How to access MySQL Database Using JSP

Source: Internet
Author: User

<% @ Page import = "java. SQL .*"
Import = "java. util .*"
Import = "java. io .*"
ContentType = "text/html; charset = gb2312"
%>
<Html>
<Head>
<Title> use a MYsql database </title>
<Meta name = "GENERATOR" content = "Microsoft FrontPage 4.0">
<Meta name = "ProgId" content = "FrontPage. Editor. Document">
</Head>
<Body>
<P align = "center"> <u> <font size = "5" face = "文 "> use JSP to read Mysql Databases </font> </u> </p>
<%!
String host = "localhost"; // Database host
String database = "pinghui"; // database Name
String user = "test"; // user Name
String pass = ""; // Password
%> <% Java. SQL. Connection sqlConn; // database Connection object
Java. SQL. Statement sqlStmt; // Statement object
Java. SQL. ResultSet sqlRst; // result set object
// Register the JDBC driver object
Class. forName ("org. gjt. mm. mysql. Driver"). newInstance ();
// Connect to the database
SqlConn = java. SQL. DriverManager. getConnection ("jdbc: mysql: //" + host + "/" + database, user, pass );
// Create a statement object
SqlStmt = sqlConn. createStatement (java. SQL. ResultSet. TYPE_SCROLL_INSENSITIVE, java. SQL. ResultSet. CONCUR_READ_ONLY );
// Execute the SQL statement
String sqlQuery = "select count (*) from comment ";
SqlRst=sqlStmt.exe cuteQuery (sqlQuery );
SqlRst. next ();
Int count = sqlRst. getInt (1); // obtain the total number of message records
SqlQuery = "select * from comment order by userid desc ;";
SqlRst=sqlStmt.exe cuteQuery (sqlQuery); // execute an SQL statement
%>
There are <% = count %> messages in total
<Table border = "1" width = "100%" bordercolorlight = "# CC99FF" cellpadding = "2" bordercolordark = "# FFFFFF" cellspacing = "0">
<Tr>
<Td align = "center"> ID </td>
<Td align = "center"> nickname </td>
<Td align = "center"> gender </td>
<Td align = "center"> address </td>
<Td align = "center"> ip address </td>
<Td align = "center"> zip code </td>
<Td align = "center"> Oicq </td>
<Td align = "center"> ICQ </td>
<Td align = "center"> phone number </td>
<Td align = "center"> message </td>
<Td align = "center"> time </td>
</Tr>
<% While (sqlRst. next () {// get the next record %>
<Tr> <! -- Display record -->
<Td> <% = sqlRst. getString ("userid") %> </td>
<Td> <% = sqlRst. getString ("username") %> </td>
<Td> <% = sqlRst. getString ("sex") %> </td>
<Td> <% = sqlRst. getString ("address") %> </td>
<Td> <% = sqlRst. getString ("ip") %> </td>
<Td> <% = sqlRst. getString ("post") %> </td>
<Td> <% = sqlRst. getString ("oicq") %> </td>
<Td> <% = sqlRst. getString ("icq") %> </td>
<Td> <% = sqlRst. getString ("telnumber") %> </td>
<Td> <% = sqlRst. getString ("comment") %> </td>
<Td> <% = sqlRst. getString ("time") %> </td>
</Tr>
<% }%>
</Table>
</Body>
<%
// Close the result set object
SqlRst. close ();
// Close the statement object
SqlStmt. close ();
// Close the database connection
SqlConn. close ();
%>

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.