熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

java程序操作MySQL數據庫

2022-06-13   來源: JSP教程 

  數據庫內容

java程序操作MySQL數據庫   java源代碼(代碼實現的是查詢成績為的人員信息至於其他功能的 代碼中有注釋) 注意在eclipse裡運行程序的時候要工程裡插入jar包否則運行異常! import javasql*;
import javaio*; class database_manage {  public Connection conn = null;  public ResultSet rs = null;  private String DatabaseDriver = "commysqljdbcDriver";
 // DataSource 數據源名稱DSN
 private String DatabaseConnStr = "jdbc:mysql://localhost:/people_manage?useUnicode=true&characterEncoding=utf"
 + "rootroot";  public void setDatabaseDriver(String Driver) {
thisDatabaseDriver = Driver;
 }  public String getDatabaseDriver() {
return (thisDatabaseDriver);
 }  public void setDatabaseConnStr(String ConnStr) {
thisDatabaseConnStr = ConnStr;
 }  public String getDatabaseConnStr() {
return (thisDatabaseConnStr);
 }  public database_manage() {// 構造函數連接數據庫
try {
 ClassforName(DatabaseDriver);
} catch (javalangClassNotFoundException e) {
 Systemerrprintln("加載驅動器有錯誤:" + egetMessage());
 Systemoutprint("執行插入有錯誤:" + egetMessage());// 輸出到客戶端
}
 }
 public ResultSet query(String sql) {// 查詢數據庫
rs = null;
try {
 conn = DriverManager
 getConnection(
 "jdbc:mysql://localhost:/people_manage?useUnicode=true&characterEncoding=utf"
 "root" "root");
 Statement stmt = conncreateStatement();
 rs = stmtexecuteQuery(sql);
} catch (SQLException ex) {
 Systemerrprintln("執行查詢有錯誤:" + exgetMessage());
 Systemoutprint("執行查詢有錯誤:" + exgetMessage()); // 輸出到客戶端
}

  return rs;

 }  public int update_database(String sql) {// 更新或插入數據庫
int num = ;
try {
 conn = DriverManager
 getConnection(
 "jdbc:mysql://localhost:/people_manage?useUnicode=true&characterEncoding=utf"
 "root" "root");
 Statement stmt = conncreateStatement();
 num = stmtexecuteUpdate(sql);
} catch (SQLException ex) {
 Systemerrprintln("執行插入有錯誤:" + exgetMessage());
 Systemoutprint("執行插入有錯誤:" + exgetMessage());// 輸出到客戶端
}  CloseDataBase();
return num;  }  public void CloseDataBase() {// 關閉數據庫
try {
 connclose();
} catch (Exception end) {
 Systemerrprintln("執行關閉Connection對象有錯誤" + endgetMessage());
 Systemoutprint("執行執行關閉Connection對象有錯誤有錯誤:" + endgetMessage()); // 輸出到客戶端
}
 } } class people {
 private String uid;
 private String name;
 private String banji;
 private int score;  public people() {
 }  public people(String uid String name String banji) {
thisuid = uid;
thisname = name;
thisbanji = banji;
 }  public people(String uid String name String banji int score) {
thisuid = uid;
thisname = name;
thisbanji = banji;
thisscore = score;
 }  public String getUid() {
return uid;
 }  public void setUid(String uid) {
thisuid = uid;
 }  public String getName() {
return name;
 }  public void setName(String name) {
thisname = name;
 }  public String getBanji() {
return banji;
 }  public void setBanji(String banji) {
thisbanji = banji;
 }  public int getScore() {
return score;
 }  public void setScore(int score) {
thisscore = score;
 } } public class manage {  private people[] people_array;// 對象數組  public void add_people(String uid String name) {  String sql = "insert people (uidname) values (" + uid + "" + name
+ ")";// sql插入語句
// String sql = "insert people (uidname) values (uidtom)";  database_manage db_obj = new database_manage();
db_objupdate_database(sql);
 }  public void update_people(String uid String name) {  String sql = "update people set name=" + name + " where uid=" + uid
+ "";
database_manage db_obj = new database_manage();
db_objupdate_database(sql);  }  public void delete_people(String uid) {  String sql = "delete from people  where uid=" + uid + "";  database_manage db_obj = new database_manage();  db_objupdate_database(sql);  }  public people query_people(String uid) {
database_manage db_obj = new database_manage();
// String adminid=null;
String uid_new name banji;
uid_new = null;
name = null;
banji = null;
String sql_query = "select * from people where uid=" + uid + "";  try {
 ResultSet rs = db_objquery(sql_query);
 if (rsnext()) {
uid_new = rsgetString("uid");
name = rsgetString("name");
banji = rsgetString("banji");
 }
} catch (Exception e) {
 egetMessage();
}
people new_people = new people(uid_new name banji);
return new_people;
 }  public people[] query_people_byscore(int score) {
database_manage db_obj = new database_manage();
String uid_new name banji;
uid_new = null;
name = null;
banji = null;
int score_new = ;

  String sql_query = "select * from people where score=" + score;// sql查詢語句

  try {

  ResultSet rs = db_objquery(sql_query);// 查詢後返回結果集

  int num = ;

  ResultSet rs_new = rs;

  while (rs_newnext()) {// 統計結果集中學生個數

  num++;

  }

  // Systemoutprintln(num);

  people_array = new people[num];

  int i = ;

  rsbeforeFirst();// 返回結果集的開始

  while (rsnext()) {

  uid_new = rsgetString("uid");

  name = rsgetString("name");

  banji = rsgetString("banji");

  score_new = rsgetInt("score");

  people_array[i] = new people(uid_new name banji score_new);

  i++;

  }

  } catch (Exception e) {

  egetMessage();

  }

  return people_array;

  }

  public static void main(String args[]) {

  /*

  * people new_people=new people();

  *

  * manage mr=new manage(); //mradd_people("""小明");插入一個學生的信息

  *

  * new_people=mrquery_people("");//查詢uid=的學生信息返回對象 Systemout

  * println(""+new_peoplegetName()+"  "+new_peoplegetBanji());

  * mrupdate_people("" "小王");更新一個學生的信息

  *

  * new_people=mrquery_people("");//更新後查詢

  *

  * Systemoutprintln(""+new_peoplegetName()+"  "+new_peoplegetBanji(

  * ));

  */

  manage mr = new manage();

  // mrdelete_people("");刪除uid=的學生信息

  people[] people_array;// 聲明對象數組

  people_array = mrquery_people_byscore();// 返回成績為一百的學生類數組後輸出

  int num = ;

  num = people_arraylength;

  for (int i = ; i < num; i++) {

  Systemoutprintln(people_array[i]getUid() + " "

  + people_array[i]getName() + "  "

  + people_array[i]getBanji() + "  "

  + people_array[i]getScore());

  }

  }

  }

  程序運行結果


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20030.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.