How to deal with JSP connection to the MySQL database

Source: Internet
Author: User

When you are faced with JSP connection to the MySQL database, you first need to create a username table in the MySQL database, and create two character fields in the table, the field names are: uid, pwd, insert several pieces of test data.

The following two methods are used to connect a JSP to a MySql database:

The first method is implemented using JSP:

<% @ Page contentType = "text/html;
Charset = gb2312 "language =" java"
Import = "java. SQL. *" %>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<% //*******************************
****************
JDBC_ODBC is used to connect to the MySql database. You do not need to set the data source ***********
**********************/
// ********** Start the database connection code ******/
// Modify the following items by yourself: String server = "localhost ";
// MYSQL server address String dbname = "test ";
// MYSQL database name String user = "root ";
// MYSQL database login username String pass = "chfanwsp ";
// MYSQL database logon password String port = "3306 ";
// The port number of the SQL Server,
Default Value: 1433 // database connection string
String url = "jdbc: mysql: //" + server + ":" + port + "/" + dbname + "?
User = "+ user +" & passWord = "+ pass +" & useUnicode
= True & characterEncoding = GB2312 ";
// Load the Driver Class. forName ("org. gjt. mm. mysql. Driver"). newInstance ();
// Establish Connection conn = DriverManager. getConnection (url );
// Create the Statement object Statement stmt = conn. createStatement
(ResultSet. TYPE_SCROLL_SENSITIVE,
ResultSet. CONCUR_UPDATABLE );
// ***** Database connection code ended *******
String SQL = "select * from username ";
ResultSet rs1_stmt.exe cuteQuery (SQL );
// Rs. first (); while (rs. next () {out. print ("username :");
Out. print (rs. getString ("uid") + "Password :");
Out. println (rs. getString ("pwd") + "<br> ");}
Rs. close (); stmt. close (); conn. close (); %>

 

The second method is implemented using JavaBean:

The Class files compiled by DBConnMySql. java should be stored in the Web-INFclassesconn directory.

Package conn; // import the package import java. SQL .*;
// Public class DBConnMySql for database import operations
// Constructor, initializing {private Connection conn;
// Connection object private Statement stmt;
// Statement object private ResultSet rs;
// Result set object private String MySqldriver;
// MYSQL Server Driver String private String MySqlURL;
// MYSQL Server connection string //******************************** *
Driver with org. gjt. mm. mysql. Driver
* This method gets various parameters required for the connection and forms a connection string,
Then establish the connection * server; dbname, user, pass, port
Represents the address of the MYSQL server,
* Database, user name, password, Port
**********************************/
Public Connection getConnToMySql
(String server, String dbname, String user, String pass, String port)
{// MYSQl Driver MySqldriver = "org. gjt. mm. mysql. Driver ";
MySqlURL = "jdbc: mysql ://";
// Try {// complete connection string MySqlURl
= MySqlURL + server + ":" + port + "/" + dbname + "? User =
"+ User +" & password = "+ pass +" & useUnicode
= True & characterEncoding = GB2312 ";
Class. forName (MySqldriver); conn
= DriverManager. getConnection (MySqlURL );}
Catch (Exception e) {System. out. println
("Database operation error, please check carefully ");
// System. err. println (e. getMessage ();} return conn ;}
// Close the database connection public void close () {try {// rs. close ();
// Stmt. close (); conn. close ();} catch (SQLException
Sqlexception) {sqlexception. printStackTrace ();}}}

 

This file only realizes the connection to the database. Next we will write a test file to query records from the database using SQL statements to verify whether the connection to our database is successful.

The source code of the connmysql. jsp file is as follows:

<Meta http-equiv = "Content-Type" content = "text/html;
Charset = gb2312 "> <% @ page contentType =" text/html;
Charset = gb2312 "language =" java "import =" java. SQL. * "%>
<Jsp: useBean id = "DBConn" scope = "page" class = "conn. DBConnMySql"/>
<% // Modify the following items by yourself: String server = "localhost ";
// MYSQL server address String dbname = "test ";
// MYSQL database name String user = "root ";
// MYSQL database login username String pass = "chfanwsp ";
// MYSQL database logon password String port = "3306 ";
// The port number of the SQL Server. The default value is 1433 Connection.
Conn = DBConn. getConnToMySql
(Server, dbname, user, pass, port );
Statement stmt = conn. createStatement
(ResultSet. TYPE_SCROLL_INSENSITIVE,
ResultSet. CONCUR_READ_ONLY );
String SQL = "select * from username ";
String sql1 = "insert into username (uid, pwd) values
('Dream year', 'Dream year') ";stmt.exe cuteUpdate (sql1 );
ResultSet rs1_stmt.exe cuteQuery (SQL); while (rs. next ())
{Out. print ("username:"); out. print (rs. getString ("uid") + "Password :");
Out. println (rs. getString ("pwd") + "<br> ");}
// Rs. close (); // stmt. close (); // conn. close (); DBConn. 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.