java串連mysql

來源:互聯網
上載者:User

標籤:

 1 import java.sql.*; 2  3 public class MysqlTest { 4     public static void main(String[] args) { 5         // 驅動程式名 6         String driver = "org.gjt.mm.mysql.Driver"; 7         // URL指向要訪問的資料庫名world 8         // String url = "jdbc:mysql://127.0.0.1:3306/xh"; 9         String url = "jdbc:mysql://localhost/xh";10         // MySQL配置時的使用者名稱11         String user = "xiaohengdada";12         // MySQL配置時的密碼13         String password = "123456";14         String name;15         try {16             // 載入驅動程式17             Class.forName(driver);18             // 連續資料庫19             Connection conn = DriverManager.getConnection(url, user, password);20             if (!conn.isClosed())21                 System.out.println("Succeeded connecting to the Database!");22             // statement用來執行SQL語句23             Statement statement = conn.createStatement();24             // 要執行的SQL語句25             String sql = "select * from students";26 27             // 結果集28             ResultSet rs = statement.executeQuery(sql);29             System.out.println(rs);30             System.out.println("Succeeded connecting to the Database!");31             while (rs.next()) {32                 // 選擇Name這列資料33                 name = rs.getString("name");34                 // 輸出結果35                 System.out.println(name);36             }37             rs.close();//關閉當前的結果集38             statement.close();//關閉statement對象以及所對應的結果集39             conn.close();//關閉當前的串連以及釋放由它所建立的JDBC資源40         } catch (ClassNotFoundException e) {41             System.out.println("Sorry,can`t find the Driver!");42             e.printStackTrace();43         } catch (SQLException e) {44             e.printStackTrace();45         } catch (Exception e) {46             e.printStackTrace();47         }48     }49 }

需要在mysql建立相關資料庫及表。

串連成功,會輸出如下資料。

 

具體的配置可以參考此連結;http://database.51cto.com/art/201006/204217.htm

 

 

下面是擷取表中id欄位:

 1 import java.sql.*; 2  3 public class MysqlTest 4 { 5     public static void main(String[] args) 6     { 7         String driver="org.gjt.mm.mysql.Driver"; 8         String url="jdbc:mysql://localhost/xh"; 9         String user="xiaohengdada";10         String password="123456";11         //int id;12         int id1;13     14     15     try16     {17         Class.forName(driver);18         Connection conn=DriverManager.getConnection(url,user,password);19         if (!conn.isClosed())20             System.out.println("Succeeded connecting to be Database!");21         22         Statement statement=conn.createStatement();23         ResultSet rs=statement.executeQuery("select * from students");24         25         System.out.println(rs);26         while(rs.next())27         {28             id1=rs.getInt("id");29             System.out.println(id1);30         }31         rs.close();32         statement.close();33         conn.close();    34     }35     catch(ClassNotFoundException e)36     {37         System.out.println("Sorry,can‘t find the Driver!");38         e.printStackTrace();39     }40     catch(SQLException e)41     {42         e.printStackTrace();43     }44     catch(Exception e)45     {46         e.printStackTrace();47     }48     }49 }

 

java串連mysql

聯繫我們

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