欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何在Java程序中訪問mysql數(shù)據(jù)庫中的數(shù)據(jù)并進行簡單的操作

 更新時間:2016年05月18日 10:09:20   作者:unbelieveus  
這篇文章主要介紹了如何在Java程序中訪問mysql數(shù)據(jù)庫中的數(shù)據(jù)并進行簡單的操作的相關(guān)資料,需要的朋友可以參考下

在上篇文章給大家介紹了Myeclipse連接mysql數(shù)據(jù)庫的方法,通過本文給大家介紹如何在Java程序中訪問mysql數(shù)據(jù)庫中的數(shù)據(jù)并進行簡單的操作,具體詳情請看下文。

創(chuàng)建一個javaProject,并輸入如下java代碼:

 package link;
 import java.sql.*;
 /**
 * 使用JDBC連接數(shù)據(jù)庫MySQL的過程
 * DataBase:fuck, table:person;
 * 使用myeclipse對mysql數(shù)據(jù)庫進行增刪改查的基本操作。
 */
 public class JDBCTest {
  public static Connection getConnection() throws SQLException,
  java.lang.ClassNotFoundException
  {
  //第一步:加載MySQL的JDBC的驅(qū)動
  Class.forName("com.mysql.jdbc.Driver");
  //取得連接的url,能訪問MySQL數(shù)據(jù)庫的用戶名,密碼;jsj:數(shù)據(jù)庫名
  String url = "jdbc:mysql://localhost:/fuck";
  String username = "root";
  String password = "";
  //第二步:創(chuàng)建與MySQL數(shù)據(jù)庫的連接類的實例
  Connection con = DriverManager.getConnection(url, username, password);
  return con;
  }
  public static void main(String args[]) {
  try
  {
  //第三步:獲取連接類實例con,用con創(chuàng)建Statement對象類實例 sql_statement
  Connection con = getConnection();
  Statement sql_statement = con.createStatement();
  //如果同名數(shù)據(jù)庫存在,刪除
  //sql_statement.executeUpdate("drop table if exists student");
  //執(zhí)行了一個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表中插入數(shù)據(jù)
  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', )");
  //第四步:執(zhí)行查詢,用ResultSet類的對象,返回查詢的結(jié)果
  String query = "select * from person";
  ResultSet result = sql_statement.executeQuery(query);
  //顯示數(shù)據(jù)中person表中的內(nèi)容:
  System.out.println("person表中的數(shù)據(jù)如下:");
  System.out.println("------------------------");
  System.out.println("序號" + " " + "姓名" + " " + "分數(shù)");
  System.out.println("------------------------");
  //對獲得的查詢結(jié)果進行處理,對Result類的對象進行操作
  while (result.next())
  {
  int number = result.getInt("number");
  String name = result.getString("name");
  String mathsorce = result.getString("mathsorce");
  //取得數(shù)據(jù)庫中的數(shù)據(jù)
  System.out.println(" " + number + " " + name + " " + mathsorce);
  }
  //關(guān)閉連接和聲明
  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和賬號,密碼需要與你自己的相一致。

這些需要訪問的數(shù)據(jù)必須要與數(shù)據(jù)庫中的類型相互匹配,才能打印出正確的結(jié)果。

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

這時,在包下會多了一個Referenced Libraries包文件,則說明配置已經(jīng)成功。

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

以上所述是小編給大家介紹的如何在Java程序中訪問mysql數(shù)據(jù)庫中的數(shù)據(jù)并進行簡單的操作的相關(guān)知識,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論