Example of a JDBC connection under Tomcat

Source: Internet
Author: User
Tags stmt

1<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2Pageencoding= "UTF-8"Import= "Java.sql.*"%>3<! DOCTYPE html>456<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">7<title> Receive form data </title>89<body>Ten<p>Get the text box submission information: One<% A  -  -String jdbc_driver = "Com.mysql.jdbc.Driver";  theString Db_url = "Jdbc:mysql://localhost:3306/first"; -  -  -String USER = "root"; +String PASS = "******"; -   +Connection conn =NULL; AStatement stmt =NULL; at  Try{ -     //Load Driver -Class.forName ("Com.mysql.jdbc.Driver"); -  -SYSTEM.OUT.PRINTLN ("Connecting to Database ..."); -     //Create Connection inconn =drivermanager.getconnection (db_url,user,pass); -  toSYSTEM.OUT.PRINTLN ("Creating statement ..."); +     //Create Query -stmt =conn.createstatement (); the String SQL; *sql = "SELECT * FROM First.author"; $ResultSet rs =stmt.executequery (SQL);Panax Notoginseng     //Get Data -      while(Rs.next ()) { the        intid = rs.getint ("id"); +        intAge = Rs.getint ("Age"); AString author = rs.getstring ("Author"); theString represent = rs.getstring ("Represent"); +  -System.out.print ("ID:" +ID); $System.out.print (", Age:" +Age ); $System.out.print (", Author:" +author); -System.out.println (", Represent:" +represent); -     } the rs.close (); - stmt.close ();Wuyi conn.close (); the}Catch(SQLException se) { - se.printstacktrace (); Wu}Catch(Exception e) { - e.printstacktrace (); About}finally{ $     Try{ -        if(stmt!=NULL) - stmt.close (); -}Catch(SQLException se2) { A     } +     Try{ the        if(conn!=NULL) - conn.close (); $}Catch(SQLException se) { the se.printstacktrace (); the     } the  } theSystem.out.println ("goodbye!"); -  in%> the</body> the

Example of a JDBC connection under Tomcat

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.