JAVA-1.串連資料庫

來源:互聯網
上載者:User

標籤:mysql   java   pre   throws   before   generate   sele   except   root   

 1 import java.sql.*; 2 import com.mysql.jdbc.Driver; 3 /* 4  * 《JDBC基礎篇》 5  * 串連資料庫一般來說分為6個步驟: 6  *1-載入驅動 7  *2-建立串連 8  *3-建立語句 9  *4-執行語句10  *5-處理結果11  *6-關閉資源12  *13  */14 public class Jdbc {15         //JDBC = Java Database Connection (用Java程式串連資料庫並操作)16     public static void main(String[] args) throws ClassNotFoundException, SQLException {17         // TODO Auto-generated method stub18         //1-載入驅動 合并寫法:19         Class.forName("com.mysql.jdbc.Driver");20         //2-建立串連21         //URL路徑結構:22         //協議://主機名稱或IP:連接埠/下層路徑23 24         Connection conn = DriverManager.getConnection25                 ("jdbc:mysql://localhost:3306/test","root","******");26         27         System.out.println(conn);28         //3-建立語句29         Statement st=conn.createStatement();30         //4-執行語句31         ResultSet rs=st.executeQuery("select * from qq");32         //5-處理結果33         rs.beforeFirst();//定位到首行之前34         while(rs.next()) {//判斷下一行是否有資料,並迴圈35             //用欄位編號擷取36             String name =rs.getString(1);37             int age=rs.getInt(2);38             double height=rs.getDouble(3);39             System.out.println(name+"\t"+age+"\t"+height);40         }41         //6-關閉資源42         rs.close();43         st.close();44         conn.close();45         46 /*        Java反射機制:Java語言是物件導向的,可以把Java代碼自身當做對象處理47 48         資料庫的表對應於Java類(實體),表的欄位(列)對應於類的欄位(屬性)49         所以,可以把對資料庫表的操作,對應為對類和對象的操作50 51         DAO = Data Access Object (Data Access Objects)52 */53     }54 55 }

 

JAVA-1.串連資料庫

聯繫我們

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