[JavaEE] JDBC Quick Start and javaeejdbc Quick Start

Source: Internet
Author: User

[JavaEE] JDBC Quick Start and javaeejdbc Quick Start

JDBC: Java Data Base Connectivity java Database Connection

 

1. Two JDBC packages: Interfaces

Java. SQL javax. SQL

2. JDBC database implementation

 

Add mysql-connector-java-5.0.8-bin.jar under tomcat directory this database driver package

 

Package com. tsh. web; import java. io. IOException; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. statement; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse;/***/public class DbServlet extends HttpServlet {private static final long serialVersionUID = 1L;/***/public DbServlet () {super (); // TODO Auto-generated constructor stub}/***/protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {try {// register the database driver // DriverManager. registerDriver (new Driver (); // Class Using Reflection. forName ("com. mysql. jdbc. driver "); // get the database connection. When importing a package, note that you must import java. in SQL, Connection conn = DriverManager for interface programming. getConnection ("jdbc: mysql: // localhost: 3306/java", "root", "root"); // get the transmitter object Statement = conn. createStatement (); // obtain the result set object ResultSet resultSet=statement.exe cuteQuery ("select * from user"); // traverse while (resultSet. next () {String username = resultSet. getString ("username"); response. getWriter (). write (username) ;}} catch (Exception e) {e. printStackTrace () ;}/ ***/protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}}

 

 

 

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.