JSP connects to MySql database

http://www.bdqn.cn/news/201305/9104.shtml

In order to establish a connection with the database managed by the MySql database server, it must be ensured that the MySql database server has been started. If the configuration of the MySql database server has not been changed, the port occupied by the database server is 3306.

To establish a connection with the database using the pure Java database driver, you need to load the pure Java driver, and then establish a connection with the specified database.

(1) Load the pure Java driver.

You can log in to the official website of MySql http://www.mysql.com, download the driver, such as mysql-connector-java- 5.0.24.zip, extract the ZIP file to the hard disk, and go to the mysql-connector- The java-5.0.24-bin.jar file is the pure Java driver for connecting to the MySql database. Copy the driver to the jrelibext folder of the JDK used by the Tomcat server, such as D:jdk1.5jrelibext, or to the commonlib folder of the Tomcat server installation directory, such as D:apache-tomcat-5.5.20commonlib.

The application loads the MySql driver code as follows:

Try{

Class.forName(“com.mysql.jdbc.Driver”);

}

Catch(Exception e){}

(2) Establish a connection with the specified database.

Assuming that the application and the MySql server are on the same computer, the code for establishing a connection between the application and the database Car is as follows:

Try{

String uri=”jdbc:mysql://localhost:3306/Car”;

String user=”root”;

String password=”123”;

Con=DriverManager.getConnection(uri,user,password);

}

Catch(SQLException e){}

Among them, the root user has access to the database Car, and the password of the root user is 123. If the root user does not have a password set, then change the above String password=”123”; to String password=””;

Example 1 The JSP page connects to the MySql database auction and queries the auction_admin table.

linkMysql.jsp

<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%
Connection con;
Statement sql;
ResultSet rs;
try{Class.forName("com.mysql.jdbc.Driver").newInstance();}
catch(Exception e){out.print(e);}
try{
    String uri="jdbc:mysql://localhost:3306/auction";
    con=DriverManager.getConnection(uri,"root","");
    sql=con.createStatement();
    rs=sql.executeQuery("SELECT * FROM auction_user");
    out.print("<table border=2>");
    out.print("<tr>");
    out.print("<th width=100>"+"user_id");
    out.print("<th width=100>"+"username");
    out.print("<th width=100>"+"userpass");
    out.print("<th width=100>"+"email");
    out.print("</tr>");
    while(rs.next()){
        out.print("<tr>");
        out.print("<td>"+rs.getString(1)+"</td>");
        out.print("<td>"+rs.getString(2)+"</td>");
        out.print("<td>"+rs.getString(3)+"</td>");
        out.print("<td>"+rs.getString(4)+"</td>");
        out.print("</tr>");
    }
    out.print("</table>");
    con.close();
}
catch(SQLException e1){out.print(e1);}
%>
</body>
</html>

 The running result is shown in the figure:

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326567658&siteId=291194637