[Java EE] JDBC Quick Start

Source: Internet
Author: User

JDBC:java data Base Connectivity java database connection

1. Two packages that make up JDBC : Mostly interfaces

java.sql Javax.sql

2. database Implementation of the corresponding JDBC

Add mysql-connector-java-5.0.8-bin.jar This database driver package under Tomcat 's directory

 PackageCom.tsh.web;Importjava.io.IOException;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.Statement;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/** */ Public classDbservletextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; /**     */     PublicDbservlet () {Super(); //TODO auto-generated Constructor stub    }    /**     */    protected voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {Try {            //Registering Database Drivers//Drivermanager.registerdriver (New Driver ()); //How to use reflectionClass.forName ("Com.mysql.jdbc.Driver"); //to get the database connection, when the guide packet, attention to guide java.sql, interface-oriented programmingConnection conn=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/java", "root", "root"); //get The transmitter objectStatement statement=conn.createstatement (); //get result Set objectResultSet resultset=statement.executequery ("SELECT * from User"); //Traverse             while(Resultset.next ()) {String username=resultset.getstring ("username");            Response.getwriter (). write (username); }        } Catch(Exception e) {e.printstacktrace (); }    }    /**     */    protected voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {}}

[Java EE] JDBC Quick Start

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.