用JDBC串連MySQL資料庫

來源:互聯網
上載者:User

用Jdbc串連MySql伺服器還是很方便的。

首先,將jdbc匯入工程,或者將jdbc放到ClassPath裡,這裡我利用Eclipse直接匯入jdbc jar檔案,不羅嗦了。

然後,制定DriverManager,利用最簡單的方法,Class類的froName直接完成,代碼:

Class.forName("com.mysql.jdbc.Driver").newInstance();

然後,執行個體化一個連結Connection,注意使用者名稱和密碼,有幾個方法可供選擇,這裡我用的是DirverManager類的getConnection(String url, String user, String password)方法。具體使用:DriverManager

例如:Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/mydatabase", "root", "1234");

下一步,建立用於執行sql語句的Statement,這個容易,一行代碼搞定:

Statement stat=conn.createStatement();

最後就可以利用stat執行個體執行sql語句了,具體參考:Statement

範例程式碼:

建立的mydatabase資料庫中有一個mytable表,此表包含一個integer的id和一個text的content。

利用一下代碼查看mytable表中的前20行的content部分。

  1. package com.tobacco.mysqltest;  
  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 Main {  
  10.     private static Connection conn;  
  11.     private static Statement stat;  
  12.     private static ResultSet rs;  
  13.   
  14.       
  15.     public static void main(String[] args) {  
  16.         try {  
  17.             Class.forName("com.mysql.jdbc.Driver").newInstance();  
  18.             System.out.println("load jdbc successfully");  
  19.         } catch (InstantiationException e) {  
  20.             // TODO Auto-generated catch block   
  21.             e.printStackTrace();  
  22.         } catch (IllegalAccessException e) {  
  23.             // TODO Auto-generated catch block   
  24.             e.printStackTrace();  
  25.         } catch (ClassNotFoundException e) {  
  26.             // TODO Auto-generated catch block   
  27.             e.printStackTrace();  
  28.         }  
  29.           
  30.         try {  
  31.             conn=DriverManager.getConnection("jdbc:mysql://localhost/mydatabase", "root", "1234");  
  32.             stat=conn.createStatement();  
  33.             int n=20;  
  34.             int i=1;  
  35.             while(i<n){  
  36.                 rs = stat.executeQuery("SELECT * FROM mytable WHERE id="+i);  
  37.                 if(rs!=null){  
  38.                     rs.first();  
  39.                     String content=rs.getString(rs.findColumn("content"));  
  40.                     System.out.println(content);  
  41.                 }  
  42.                 i++;  
  43.             }  
  44.               
  45.         } catch (SQLException e) {  
  46.             // TODO Auto-generated catch block   
  47.             e.printStackTrace();  
  48.         }  
  49.           
  50.   
  51.     }  
  52.   
  53. }  

相關文章

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.