js| Access | data | database | questions
First, JSP access to the database method:
1. Create your own virtual directory, create a Web-inf\lib,web-inf\class,web-inf\jar under the virtual directory, and each directory description is as follows:
Web-inf\web.xml Configure access to the servlet and configure some initial parameters in the file, examples are as follows:
<servlet>
<servlet-name>TestServlet</servlet-name> <!--servlet name-->
<servlet-class>com.myweb.TestServlet</servlet-class> <!--The name of the Servlet class includes the package name-->
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name> <!--servlet name-->
<url-pattern>/Servlet/TestServlet</url-pattern> <!--the URL to access the Servlet is relative to the root directory-->
</servlet-mapping>
</servlet>
Web-inf\lib stores the jars that are accessed in Jsp,javabean, including the drivers of some of the databases we use (not Java itself, such as MySQL).
Web-inf\classes is the class file that is used to store in Jsp,javabean. If it is a package, the class is stored according to the package's directory.
Second, JSP access to the database of some problems and solutions:
1. Class can not find the problem: the main use of the class is not stored in the appropriate location, the jar stored in Lib, class files stored under class.
Third, access to the database instance: JSP access MySQL
1. Create the directory test and create the relevant directory below. Web-inf, Web-inf\lib.
2. Configure the virtual directory to point to test.
3. Will Mysql-conn. The jar is copied below lib.
4. The 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 ();
Connecting to a database
sqlconn= java.sql.DriverManager.getConnection ("jdbc:mysql://localhost:3306/" +database,user,pass);
To 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: To access a database, you must first confirm that you have the appropriate permissions.