[javaEE] JDBC快速入門

來源:互聯網
上載者:User

標籤:

JDBC:Java Data Base Connectivity java資料庫連接

 

1.組成JDBC的兩個包:主要是介面

java.sql javax.sql

2.相應JDBC的資料庫實現

 

在tomcat的目錄下面添加mysql-connector-java-5.0.8-bin.jar這個資料庫驅動包

 

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 {            // 註冊資料庫驅動            //DriverManager.registerDriver(new Driver());            //使用反射的方式            Class.forName("com.mysql.jdbc.Driver");            //擷取資料庫連接,導包的時候,注意要導java.sql下的,面向介面編程            Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/java","root","root");            //擷取傳輸器對象            Statement statement=conn.createStatement();            //擷取結果集對象            ResultSet resultSet=statement.executeQuery("select * from user");            //遍曆            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 {    }}

 

 

 

[javaEE] JDBC快速入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.