The problem collation of JSP accessing database

Source: Internet
Author: User
Tags mysql root directory
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.



Related Article

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.