編寫一個簡單的jdbc例子程式

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   os   io   art   

 1 package it.cast.jdbc; 2  3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8  9 public class Base {10 11     public static void main(String[] args) throws SQLException {12          test();13     }14 15     static void test() throws SQLException {16         // 1.註冊驅動17         DriverManager.registerDriver(new com.mysql.jdbc.Driver());18 19         // 2.建立串連20         Connection conn = DriverManager.getConnection(21                 "jdbc:mysql://localhost:3306/jdbc", "root", "123");22 23         // 3.建立語句24         Statement st = conn.createStatement();25 26         // 4.執行語句27         ResultSet rs = st.executeQuery("select * from user");28 29         // 5.處理結果30         while (rs.next()) {31             System.out.println(rs.getObject(1) + "\t" + rs.getObject(2) + "\t"32                     + rs.getObject(3)+"\t" + rs.getObject(4));33         }34         35         //6.釋放資源36         rs.close();37         st.close();38         conn.close();39     }40 41 }
BASE

匯入jar ==>mysql-connector-java-5.1.22-bin.jar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.