SQL---->mySQl資料庫1------jdbc簡單入門

來源:互聯網
上載者:User

標籤:mysql資料庫   nbsp   base   串連   res   驅動   row   http   image   

JDBC(Java Data Base Connectivity,java資料庫連接)是一種用於執行SQL語句的Java API,可以為多種關聯式資料庫提供統一訪問。

目的:不用學習每個資料庫的驅動,學習jdbc介面就好了

下面我就舉例一個小demo,串連mysql資料庫,查詢到表中資料:

首先,我的mysql資料庫中資料:

用jdbc,我們要引入mysql-connector-java-5.1.41-bin.jar

/** *  */package cn.snowing;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/** * @author: snowing * @date : 2017年4月27日 *  */public class JdbcDemo1 {public static void main(String[] args) throws SQLException, ClassNotFoundException {String url = "jdbc:mysql://localhost:3306/mydb1";//這樣也行,預設連接埠//String url = "jdbc:mysql:///mydb1";String user = "root";String password = "1";// 1.載入驅動註冊了兩次驅動 //DriverManager.registerDriver(new com.mysql.jdbc.Driver());//2.載入驅動常用方式,只註冊了一次驅動Class.forName("com.mysql.jdbc.Driver");// 2.擷取串連Connection conne = DriverManager.getConnection(url, user, password);// 3.擷取像資料庫發送sql語句的statament對象Statement st = conne.createStatement();// 4.向資料庫發送sql,擷取資料庫返回的結果集ResultSet rs = st.executeQuery("select * from user;");// 5.從介面集中擷取資料while (rs.next()) {System.out.println("" + rs.getObject("id"));System.out.println(rs.getObject("username"));System.out.println(rs.getObject("birthday"));System.out.println(rs.getObject("entry_date"));System.out.println(rs.getObject("job"));System.out.println(rs.getObject("salary"));System.out.println(rs.getObject("resume"));System.out.println(rs.getObject("image"));}// 6.釋放串連,很重要啊,不要忘記!!rs.close();st.close();conne.close();}}

 

結果:

 

SQL---->mySQl資料庫1------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.