Abstract: JSP database access to problems
<textarea Readonly style="border:none;font-family:Courier New;line-height:150%;width:760px;overflow-y:visible"> a JSP Database Access Method: 1. Build their own virtual directory, virtual directory Below the establishment of web-inf \ lib, web-inf \ class, web-inf \ jar, various directory described as follows: web-inf \ web.xml configuration in the document, as well as configuration visit Servlet some of the initial parameters, the following examples: TestServlet Com.myweb.TestServlet TestServlet / Servlet / TestServlet Web-inf \ lib stored in jsp, javabean visit in the jar, including our use of a number of database drivers (not java support itself, such as the mysql). Web-inf \ classes stored in jsp, javabean like to use is the class documents. If the packet, according to the package directory will be a storage class. Second, JSP database access some of the problems and their solutions: 1. Class no question: is the class not to use stored in suitable location in the storage jar lib, the class documents stored in the class. Third, access the database examples: JSP visit Mysql 1. Creating the directory test, below the development of the directory. Web-inf, web-inf \ lib 2. Configuration at the test virtual directory 3. Will be mysql-conn. Below copy to jar lib. 4. Test \ dbtext.jsp following code: <% @ page language = "java"%> <% @ page contentType = "text / html; gb2312 charset ="%> <% @ 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 (); / / link database sqlConn = java.sql.DriverManager.getConnection ( "jdbc: mysql: / / localhost: 3306 /" + database, user, pass); / / create objects statement sqlConn.createStatement sqlStmt = () ; / / implementation 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: first visit to a database to confirm the corresponding authority. </ Textarea>
↑ Back
Share and Enjoy:
These icons link to social bookmarking sites where readers can share and discover new web pages.
Tags: java access, JSP
Releated Java Articles
March 15, 2007 | Filed Under
Java Server Pages |
Comments