JSP connection to the Mysql database I wrote a code to connect to the MySQL database using JSP.
To use this code correctly, you must first 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 to the MySql database through JSP.
The first method is implemented using JSP.
<% @ Page contentType = "text/html; charset = gb2312" language = "java"
Import = "java. SQL. *" %>
<%
//**************************************
* ******* JDBC_ODBC connects to the MySql database and does not need to set a data source
*********************************/
// ********** Start the database connection code ******/
// Modify the following items
String server = "localhost"; // address of the MYSQL server
String dbname = "test"; // MYSQL database name
String user = "root"; // logon username of the MYSQL database
String pass = "chfanwsp"; // MYSQL database logon password
String port = "3306"; // the port number of the SQL Server. the default value is 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 a connection
Connection conn = DriverManager. getConnection (url );
// Create a 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") +" ");
}
Rs. close ();
Stmt. close ();
Conn. close ();
%>
|
The second method is implemented using JavaBean. See the code:
DBConnMySql. java
The compiled Class file should be placed in the WEB-INF/classes/conn directory.
Package conn; // import the package
Import java. SQL. *; // database import operation class Public class DBConnMySql // Constructor, initialization
{
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 the various parameters required for the connection, forming a connection string, and then establishing a connection
* Server; dbname, user, pass, and port indicate the addresses of MYSQL servers respectively,
* 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: //"; // part of the connection string
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 implements database connection. next we will write another test file.
It is to use SQL statements to query records from the database to verify whether the connection to our database is successful.
The source code of the connmysql. jsp file is as follows:
<% @ Page contentType = "text/html; charset = gb2312" language = "java"
Import = "java. SQL. *" %>
<%
// Modify the following items
String server = "localhost"; // address of the MYSQL server
String dbname = "test"; // MYSQL database name
String user = "root"; // logon username of the MYSQL database
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") +" ");
}
// Rs. close ();
// Stmt. close ();
// Conn. close ();
DBConn. close ();
%>
|