MyEclipse+Mysql (二),myeclipsemysql

來源:互聯網
上載者:User

MyEclipse+Mysql (二),myeclipsemysql

上一節介紹了如何在Myeclipse中串連mysql

這一節介紹如何在java程式中訪問mysql資料庫中的資料b並進行簡單的操作

 

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

 1 package link; 2 import java.sql.*; 3  4 /** 5 * 使用JDBC串連資料庫MySQL的過程 6 * DataBase:fuck, table:person; 7 * 使用myeclipse對mysql資料庫進行增刪改查的基本操作。 8 */ 9 10 public class JDBCTest {11     public static Connection getConnection() throws SQLException,12     java.lang.ClassNotFoundException13     {14     //第一步:載入MySQL的JDBC的驅動15     Class.forName("com.mysql.jdbc.Driver");16     17     //取得串連的url,能訪問MySQL資料庫的使用者名稱,密碼;jsj:資料庫名18     String url = "jdbc:mysql://localhost:3306/fuck";19     String username = "root";20     String password = "123";21     22     //第二步:建立與MySQL資料庫的串連類的執行個體23     Connection con = DriverManager.getConnection(url, username, password);24     return con;25     }26     27     28     public static void main(String args[]) {29     try30     {31     //第三步:擷取串連類執行個體con,用con建立Statement對象類執行個體 sql_statement32     Connection con = getConnection();33     Statement sql_statement = con.createStatement();34     35     //如果同名資料庫存在,刪除36     //sql_statement.executeUpdate("drop table if exists student");37     //執行了一個sql語句產生了一個名為student的表38     //sql_statement.executeUpdate("create table student (id int not null auto_increment, name varchar(20) not null default 'name', math int not null default 60, primary key (id) ); ");39     40     //向person表中插入資料41     sql_statement.executeUpdate("insert person values(3, 'liying', 98)");42     sql_statement.executeUpdate("insert person values(4, 'jiangshan', 88)");43     sql_statement.executeUpdate("insert person values(5, 'wangjiawu', 78)");44     sql_statement.executeUpdate("insert person values(6, 'duchangfeng', 100)");45     46     //第四步:執行查詢,用ResultSet類的對象,返回查詢的結果47     String query = "select * from person";48     ResultSet result = sql_statement.executeQuery(query);49     50     //顯示資料中person表中的內容:51     System.out.println("person表中的資料如下:");52     System.out.println("------------------------");53     System.out.println("序號" + " " + "姓名" + " " + "分數");54     System.out.println("------------------------");55     56     //對獲得的查詢結果進行處理,對Result類的對象進行操作57     while (result.next())58     {59     int number = result.getInt("number");60     String name = result.getString("name");61     String mathsorce = result.getString("mathsorce");62     63     //取得資料庫中的資料64     System.out.println(" " + number + " " + name + " " + mathsorce);65     }66     67     //關閉串連和聲明68     sql_statement.close();69     con.close();70     71     } catch(java.lang.ClassNotFoundException e) {72     System.err.print("ClassNotFoundException");73     System.err.println(e.getMessage());74     } catch (SQLException ex) {75     System.err.println("SQLException: " + ex.getMessage());76     }77     }78     79     }

————————————————————————————————————————————————————————————————————————————————————————

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

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

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

——————————————————————————————————————————————————————————————————————————————————————————

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

___________________________________________________________________________________________________________________________________________________________

 

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

——————————————————————————————————————————————————————————————————————————————————————————

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

 

聯繫我們

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