java之数据管理系统软件
學習java數(shù)據(jù)庫的基礎操作:連接,增刪查改;css的廣泛應用,在此基礎上,數(shù)據(jù)管理系統(tǒng)軟件是這些知識點的綜合項目
為了掌握和牢固java數(shù)據(jù)庫的知識點,在空閑之余寫了這個軟件,數(shù)據(jù)庫使用的是Mysql小型數(shù)據(jù)庫,需要安裝WAMP軟件。也可以使用大型數(shù)據(jù)庫甲骨文數(shù)據(jù)庫,不知為何,中型的SQL Serve 2012數(shù)據(jù)庫我一直沒有連接成功。
本軟件完成所需要的材料:
數(shù)據(jù)管理系統(tǒng)軟件可以讓用戶自定義需要創(chuàng)建的表,自定義表的字段的類型,屬性,輕松插入數(shù)據(jù),修改數(shù)據(jù),查找數(shù)據(jù),刪除數(shù)據(jù),利用各種統(tǒng)計圖選擇屬性進行比較和走向趨勢。例如管理學生信息,成績和績點比較,還可以將數(shù)據(jù)導出成為表格文件并利用打印機打印出來。
java數(shù)據(jù)庫的連接
java連接mysql數(shù)據(jù)庫需要相關的驅動,因為要連接mysql數(shù)據(jù)庫,需要導入相關的封裝好的類就是封裝好的jar包,叫sqljdbcxx.jar,由于jdk版本不一樣,所以需要不一樣的xx版本對應的jar,在這里我使用的是sqljdbc42.jar,各種版本百度隨便一搜就有了.
wamp狀態(tài)為綠色則表示配置ok,可以正常使用sql服務,圖如下
做好這些準備后我們就可以嘗試連接數(shù)據(jù)庫了
- 我們先登入到mysql數(shù)據(jù)庫,新建一個數(shù)據(jù)庫,然后創(chuàng)建一個表,再往其中插入數(shù)據(jù)
java連接數(shù)據(jù)庫的代碼部分為:
如果沒有問題(返回的ct不為空),那么將成功查詢到greens中的數(shù)據(jù)
在軟件中,我使用了用戶輸入來登入并連接數(shù)據(jù)庫的輸入框
登入成功后將顯示連接的數(shù)據(jù)庫和表
右邊的表選擇器有這個數(shù)據(jù)庫的所有表
java數(shù)據(jù)庫的增加(新建數(shù)據(jù)庫)
首先 ,我們需要知道新建數(shù)據(jù)庫的java代碼,如下:
sqlCommand = "Create database "+newDB; //新建數(shù)據(jù)庫的名 //先連接數(shù)據(jù)庫 /*..上面的代碼..*/ //連接成功后執(zhí)行語句,更上面一樣 ct.createStatement().execute(sqlCommand); //執(zhí)行了之后新建的數(shù)據(jù)庫就在
java數(shù)據(jù)庫之刪表操作
//先進行連接數(shù)據(jù)庫String SQLcommand = "delete from "+tab+" where "+column1+" = "+column1Value+";"; //然后執(zhí)行SQLcommand 語句 ct.createStatement().execute(SQLcommand);這樣 成績表就刪完了
java刪表的數(shù)據(jù)的操作
一般來說,表的第一個字段就是它的id,id是每個數(shù)據(jù)的身份證,在數(shù)據(jù)庫中找到它并且執(zhí)行語句就可以將id符合的刪除.函數(shù)返回了一個字符串用來告訴用戶刪除是否成功,函數(shù)代碼如下:
public String deleteTableData(String user,String pass,String db,String tab,String column1,String column1Value) {// 用戶賬號 密碼 數(shù)據(jù)庫 表 字段名 字段值//大多數(shù)默認情況是第一列是身份證標識,不可以存在重復的數(shù)據(jù),不然將匹配多個。String result = "刪除成功!";String dbURL = "jdbc:mysql://localhost:3306/"+db+"?seUnicode=true&characterEncoding=UTF8";String SQLcommand = "delete from "+tab+" where "+column1+" = "+column1Value+";"; //主要sql語句知識點Connection ct = null;try {ct = DriverManager.getConnection(dbURL, user, pass);if(ct!=null) {ct.createStatement().execute(SQLcommand);result = "刪除完畢!";}} catch (SQLException e) {result = "SQL連接失敗";}return result;}
進入刪除模式
id為15和19的已刪除
java的建表操作,創(chuàng)建一個新的表
建表比較復雜,它需要收集用戶建立的字段數(shù)和字段屬性,字段屬性長度,數(shù)據(jù)判斷是否合法,類型之間的關系
代碼如下:
結果如下:
系統(tǒng)會自動判斷表名和每個輸入框的值,數(shù)據(jù)是否符合要求
填寫一些屬性
點擊確定時提示建的表的所有字段
這時候我們返回到主界面查看所建立的表
新的表已經(jīng)建好
java數(shù)據(jù)庫操作之增加數(shù)據(jù)
新的表建好了,我們可以往這個表添加數(shù)據(jù),java的數(shù)據(jù)庫增加數(shù)據(jù)代碼主要如下:
//最基本的知識點String result = "執(zhí)行成功,成功插入";Connection ct = null;String sqlCommand = "insert into "+tab+" values(";for(int i = 0;i<insert_column_data.size();i++) {if(i <insert_column_data.size()-1)sqlCommand = sqlCommand+insert_column_data.get(i)+",";elsesqlCommand = sqlCommand+insert_column_data.get(i)+")";}//sqlCommand是 將要執(zhí)行的sql語句//下面進行連接數(shù)據(jù)庫 和 執(zhí)行sqlCommand語句String db_reference = "jdbc:mysql://localhost:3306/"+db+"?useUnicode=true&characterEncoding=UTF8";try {ct = DriverManager.getConnection(db_reference, user, pass);if(ct!=null) {try {ct.createStatement().execute(sqlCommand);}catch(Exception error2) {result = "執(zhí)行的時候出錯,請檢查數(shù)據(jù)合法性";}}else {result = "數(shù)據(jù)庫出差錯了。";}}catch(Exception e) {result = "連接數(shù)據(jù)庫時出錯了。";}return result;}我們使用GUI界面編程的時候怎么知道這個表有多少個字段 或者字段類型 字段長度呢?
代碼如下:
三個方法具體實現(xiàn)如下
public List<String>getColumnName(String dbURL_2,String name_2,String pass_2,String sql2){//參數(shù): 數(shù)據(jù)庫 用戶名 密碼 表List<String>name= new ArrayList<>(); //返回所有字段名字Connection ct = null;String dbURL="jdbc:mysql://localhost:3306/"+dbURL_2+"?useUnicode=true&characterEncoding=UTF8";ResultSet re2 = null;sql2 = "select * from "+sql2+";";try {try {try {Class.forName("com.microsoft.mysql.jdbc.mysqlDriver").newInstance();System.out.println("連接成功、");} catch (InstantiationException e) {} catch (IllegalAccessException e) {}//System.out.println("加載數(shù)據(jù)庫成功");} catch (ClassNotFoundException e) {//System.out.println("加載數(shù)據(jù)庫失敗了");}ct = DriverManager.getConnection(dbURL,name_2,pass_2);} catch (SQLException e) {//System.out.println("連接失敗:\n"+e.getMessage()+"\n"+e.getSQLState()+"\n"+e.getErrorCode()+"\n"+e.getLocalizedMessage());}if(ct!=null) {try {re2 = ct.createStatement().executeQuery(sql2);ResultSetMetaData data = re2.getMetaData();for(int i = 0;i<data.getColumnCount();i++) {name.add(data.getColumnName(i+1));}} catch (SQLException e) {re2 = null;}try {ct.close();} catch (SQLException e) {System.out.println("關閉失敗");}}return name;} public ArrayList<String>each_getColumn_type(String dbURL_2,String name_2,String pass_2,String sql2){// 數(shù)據(jù)庫 用戶名 密碼 表ArrayList<String> resu = new ArrayList<>();Connection ct = null;String dbURL="jdbc:mysql://localhost:3306/"+dbURL_2+"?useUnicode=true&characterEncoding=UTF8";ResultSet re2 = null;sql2 = "select * from "+sql2+";";try {ct = DriverManager.getConnection(dbURL,name_2,pass_2);} catch (SQLException e) {System.out.println("出錯");}if(ct!=null) {try {re2 = ct.createStatement().executeQuery(sql2);ResultSetMetaData data = re2.getMetaData();for(int i = 0;i<data.getColumnCount();i++) {resu.add(data.getColumnTypeName(i+1));}} catch (SQLException e) {re2 = null;}try {ct.close();} catch (SQLException e) {System.out.println("關閉失敗");}}return resu;} public ArrayList<String>each_getColumn_type_size(String dbURL_2,String name_2,String pass_2,String sql2){// 數(shù)據(jù)庫 用戶名 密碼 表ArrayList<String> resu = new ArrayList<>();Connection ct = null;String dbURL="jdbc:mysql://localhost:3306/"+dbURL_2+"?useUnicode=true&characterEncoding=UTF8";ResultSet re2 = null;sql2 = "select * from "+sql2+";";try {ct = DriverManager.getConnection(dbURL,name_2,pass_2);} catch (SQLException e) {System.out.println("出錯");}if(ct!=null) {try {re2 = ct.createStatement().executeQuery(sql2);ResultSetMetaData data = re2.getMetaData();for(int i = 0;i<data.getColumnCount();i++) {resu.add(String.format("%d",data.getColumnDisplaySize(i+1)));// System.out.println(String.format("%d",data.getColumnDisplaySize(i+1)));}} catch (SQLException e) {re2 = null;}try {ct.close();} catch (SQLException e) {System.out.println("關閉失敗");}}return resu;}三個函數(shù)返回的都是裝滿了數(shù)據(jù)的arrayList<‘string’>數(shù)組,得到了就可以確定輸入數(shù)據(jù)的字段數(shù)是多少了,
每個字段的類型是什么,長度限制等等
結果如下:
系統(tǒng)自動判斷了該表有8個字段 ,每個字段的類型 ,每個字段類型的長度,下面進行添加數(shù)據(jù)
好了,數(shù)據(jù)插入成功有提示,提示 或者不提示你成功插入數(shù)據(jù) 可以在設置里改(后面有說明)
刷新后我們就可以看到插入的數(shù)據(jù)
java數(shù)據(jù)庫之查找
有時候需要查找并且刪除詳細資料,代碼流程如下:
//連接數(shù)據(jù)庫 //執(zhí)行語句 //獲取結果結果如下:
java 持久化操作之 設置和配置
為了讓用戶體驗更好 可以對軟件進行一些屬性設置
可以設置字體和大小
背景和皮膚
目錄
Boss鍵
一些限制設置,按鈕風格,各種顏色
總的來說,消化這些知識點是需要一定時間的,如果有什么功能想知道怎么實現(xiàn)或者需要代碼包jar學習的
可以留言
總結
以上是生活随笔為你收集整理的java之数据管理系统软件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DBCP连接池常用参数详解
- 下一篇: 基于fpga的微电网模拟系统