如何在Java程式中訪問mysql資料庫中的資料並進行簡單的操作_Mysql

來源:互聯網
上載者:User

在上篇文章給大家介紹了Myeclipse串連mysql資料庫的方法,通過本文給大家介紹如何在Java程式中訪問mysql資料庫中的資料並進行簡單的操作,具體詳情請看下文。

建立一個javaProject,並輸入如下java代碼:

 package link; import java.sql.*; /** * 使用JDBC串連資料庫MySQL的過程 * DataBase:fuck, table:person; * 使用myeclipse對mysql資料庫進行增刪改查的基本操作。 */ public class JDBCTest {  public static Connection getConnection() throws SQLException,  java.lang.ClassNotFoundException  {  //第一步:載入MySQL的JDBC的驅動  Class.forName("com.mysql.jdbc.Driver");  //取得串連的url,能訪問MySQL資料庫的使用者名稱,密碼;jsj:資料庫名  String url = "jdbc:mysql://localhost:/fuck";  String username = "root";  String password = "";  //第二步:建立與MySQL資料庫的串連類的執行個體  Connection con = DriverManager.getConnection(url, username, password);  return con;  }  public static void main(String args[]) {  try  {  //第三步:擷取串連類執行個體con,用con建立Statement對象類執行個體 sql_statement  Connection con = getConnection();  Statement sql_statement = con.createStatement();  //如果同名資料庫存在,刪除  //sql_statement.executeUpdate("drop table if exists student");  //執行了一個sql語句產生了一個名為student的表  //sql_statement.executeUpdate("create table student (id int not null auto_increment, name varchar() not null default 'name', math int not null default , primary key (id) ); ");  //向person表中插入資料  sql_statement.executeUpdate("insert person values(, 'liying', )");  sql_statement.executeUpdate("insert person values(, 'jiangshan', )");  sql_statement.executeUpdate("insert person values(, 'wangjiawu', )");  sql_statement.executeUpdate("insert person values(, 'duchangfeng', )");  //第四步:執行查詢,用ResultSet類的對象,返回查詢的結果  String query = "select * from person";  ResultSet result = sql_statement.executeQuery(query);  //顯示資料中person表中的內容:  System.out.println("person表中的資料如下:");  System.out.println("------------------------");  System.out.println("序號" + " " + "姓名" + " " + "分數");  System.out.println("------------------------");  //對獲得的查詢結果進行處理,對Result類的對象進行操作  while (result.next())  {  int number = result.getInt("number");  String name = result.getString("name");  String mathsorce = result.getString("mathsorce");  //取得資料庫中的資料  System.out.println(" " + number + " " + name + " " + mathsorce);  }  //關閉串連和聲明  sql_statement.close();  con.close();  } catch(java.lang.ClassNotFoundException e) {  System.err.print("ClassNotFoundException");  System.err.println(e.getMessage());  } catch (SQLException ex) {  System.err.println("SQLException: " + ex.getMessage());  }  }  }

注意有幾個地方是你需要修改的。

如下圖中的url和帳號,密碼需要與你自己的相一致。

這些需要訪問的資料必須要與資料庫中的類型相互匹配,才能列印出正確的結果。

按右鍵工程名-->Build Path -->Configure Biuld Path -->Libraries --> Add External JARs -->加入一個jdbc包(具體請查考Mysql的簡單使用(一))--->ok

這時,在包下會多了一個Referenced Libraries包檔案,則說明配置已經成功。

點擊Run as ---> 運行Java Application --->JDBCTest--link--->顯示結果如下:

以上所述是小編給大家介紹的如何在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.