JSP access to the database

xiaoxiao2021-03-05  20

First, the method of jsp access the database:

1. Establish your own virtual directory, establish web-inf / lib, web-inf / class, web-inf / jar, and each directory in the virtual directory.

WEB-INF / Web.xml Configuring Access Servlets in this file and configures some initial parameters, as follows:

TestServlet com.myweb.testservlet

TestServlet / server / testservlet

WEB-INF / LIB is stored in JSP, JavaBean, JAR, including some databases we use (not Java itself, such as mysql).

WEB-INF / CLASSES is stored in JSP, JavaBean, is a class file. If it is a package, you have to store the Class in the directory of the package.

Second, some issues of JSP access to the database and their solutions:

1. The problem could not be found: It is mainly used by the category that is not stored in a suitable location. JAR is stored under LIB, and the class file is stored under the class.

Third, access Database instance: JSP Access MySQL

1. Establish a directory TEST to establish a related directory below. Web-inf, web-inf / lib.

2. Configure the virtual directory to point to TEST.

3. Will mysql-conn. JAR copy to the LIB below.

4. Test / dbtext.jsp code is as follows:

<% @ page language = "java"%> <% @ page contenttype = "text / html; charSet = GB2312"%> <% @ page import = "java.sql. *"%> <%

Connection Sqlconn;

ResultSet Sqlrst;

Statement SQLSTMT;

String Database = "Test";

String User = "root";

String pass = "123456";

Class.Forname ("org.gjt.mm.mysql.driver"). NewInstance (); // Connect database SQLCONN = java.sql.driverManager.getConnection ("JDBC: mysql: // localhost: 3306 /" Database, User, pass); // Create a statement object SQLSTMT = sqlconn.createstatement (); // Execute SQL statement string sqlQuery = "select * from test"; sqlrst = sqlstmt.executeQuery (sqlquery); while (sqlrst.next ()) {

Out.println (Sqlrst.getstring (1));

Out.println (SQLRST.GETSTRING (2));

}

%>

Note: Accessing a database must first confirm that there is a corresponding permissions.

转载请注明原文地址:https://www.9cbs.com/read-39089.html

New Post(0)