The JSP connection database displays the database data and the Java Connection Database call data.

Source: Internet
Author: User
Tags driver manager

JSP calls the database directly, requires the database connection related JAR package:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "UTF-8"%> <%@ page import= "java.sql.*"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >The database data queried is: <%//registered database driver Class.forName ("Oracle.jdbc.driver.OracleDriver");//Get database connection Connection conn = drivermanager.getconnection ("jdbc:oracle:thin:@172.21.2.98:1531:f3t", "SECU", "SECU"); Statement stmt = conn.createstatement (); ResultSet rs =stmt.executequery ("Select * from Hd_point")%><table border= "1" width= " ><%while" (Rs.next ()) {%><tr><td><%=rs.getstring (1)%></td><td><%=rs.getstring (2)%></td> <td><%=rs.getstring (3)%></td></tr><%}%></table></body>

Java Configuration Database Connection code (reproduced here: http://www.cnblogs.com/hanruyue/p/5977617.html):

PackageCom.hanqi.test;importJava.sql.connection;importJava.sql.drivermanager;importJava.sql.resultset;importJava.sql.sqlexception;importJava.sql.statement;public classTest {public static voidMain (string[] args) {//Access database//1. Load driver: Loads the database corresponding to the package name Oracle.jdbc.driver//1. (Load Database driver Class) Oracledriver.class try{Class.forName ("Oracle.jdbc.driver.OracleDriver"); This is a fixed//2. Gets the database connection: the Java Driver Manager//url-the database address, different database writing different 127.0.0.1 and localhost all represent native//url-database address: User- User name: password-password Connection for connection DriverManager drive manager Connection conn=Drivermanager.getconnection ("JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL", "test0816", "123456"); SYSTEM.OUT.PRINTLN ("Connection succeeded"); Operation Database-Add or delete change//3. Get Operational Database declaration Statement st=conn.createstatement ()//statement declaration createstatement Create declaration//4.DML increase Data// Perform update operation//return value represents the number of data record bars affected by the operation//int I=st.executeupdate ("INSERT into student (sno,sname,ssex)"//+ "values (' 120 ', ' Harry ', ' Male ') "); int i=st.executeupdate ("Update student set ssex= ' man ' where ssex= ' 1 '"); SYSTEM.OUT.PRINTLN ("Add data successful return value =" +i);//return value//5. query data//resultset data result set ResultSet rs=st.executequery ("SELECT * FROM Student "); Traversing the result set traversal must be a loop//next () to determine if the next record exists, move the pointer to the next record on the while(Rs.next ()) {//Read the data String sno=rs.getstring ("Sno"  ); String sname=rs.getstring ("sname"); String ssex=rs.getstring ("Ssex"); System.out.println ("sno=" +sno+ "sname=" +sname+ "ssex=" +ssex);}//Release resources//Release Resources Purpose: system resources are reduced for each program running in Windows. Some programs will consume a lot of system resources, even if the program is closed, in memory or some useless DLL files are running, which makes the system running speed down. rs.close (); St.close ();//Close Database conn.close ();} catch (ClassNotFoundException |  SQLException e) {//TODO auto-generated catch block e.printstacktrace ();          }}} 

The JSP connection database displays the database data and the Java Connection Database call data.

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.