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