查询部分
查詢(xún)備忘錄
1.??????????????功能描述
查詢(xún)數(shù)據(jù)庫(kù)信息,并打印在查詢(xún)結(jié)果頁(yè)面上,實(shí)現(xiàn)頁(yè)面清空,上一頁(yè)查詢(xún),下一頁(yè)查詢(xún),并實(shí)現(xiàn)返回主頁(yè)面
2.??????????????語(yǔ)言和環(huán)境
A、實(shí)現(xiàn)語(yǔ)言:
?? Java
B、實(shí)現(xiàn)技術(shù):
?? JavaSE、JDBC、Swing
C、環(huán)境要求:
? NetBeans 8.0.2,SQLServer2008、jdk-8u20-windows-x64
3.??????????????界面設(shè)計(jì)
標(biāo)簽,按鈕,文本框
輸入查詢(xún)數(shù)據(jù),查詢(xún)主頁(yè)面,清空文本框內(nèi)容,返回主頁(yè)面或跳轉(zhuǎn)到查詢(xún)結(jié)果頁(yè)面
顯示查詢(xún)結(jié)果,通過(guò)上一條,下一條實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的結(jié)果搜索,并實(shí)行跳轉(zhuǎn)到主頁(yè)面功能
?
提示查詢(xún)結(jié)果,數(shù)據(jù)庫(kù)無(wú)此匹配信息
?
4.??????????????數(shù)據(jù)庫(kù)設(shè)計(jì)
數(shù)據(jù)庫(kù)名:XSML
數(shù)據(jù)庫(kù)表名:Table_1
數(shù)據(jù)庫(kù)表內(nèi)容:
類(lèi)名:Username ,Id,Title,Content,Memotype,Memotime
數(shù)據(jù)庫(kù)表顯示:
5.運(yùn)行代碼
1.數(shù)據(jù)庫(kù)連接代碼
package com.dao;
?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/** *
?* @author Samuel*//*數(shù)據(jù)庫(kù)操作類(lèi)*/
public class Dao {
protected static StringdbClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
???//注意修改數(shù)據(jù)庫(kù)名稱(chēng)
???protected static String dbUrl = "jdbc:sqlserver://localhost:1434;DatabaseName=XSML";//數(shù)據(jù)庫(kù)用戶(hù)名保存在變量dbUser中
???protected static String dbUser = "sa";//數(shù)據(jù)庫(kù)密碼保存在dbPwd中
???protected static String dbPwd = "123456";
???protected static String second = null;
???private static Connection conn = null;
?
???private static Connection getConnection() {
??????? throw newUnsupportedOperationException("Not supported yet."); //To change bodyof generated methods, choose Tools | Templates.
???}
???//Dao類(lèi)的構(gòu)造方法
???Dao() {
??????? try {
??????????? if (conn == null) {
??????????????? Class.forName(dbClassName);
??????????????? conn =DriverManager.getConnection(dbUrl, dbUser, dbPwd);
??????????? } else {
??????????????? return;
??????????? }
??????????? System.out.println("conn成功!");//控制臺(tái)打印顯示連接成功
??????? } catch (Exception ee) {
??????????? ee.printStackTrace();
??????? }
?
???}
???/*數(shù)據(jù)庫(kù)查詢(xún)方法
???* 方法參數(shù):sql查詢(xún)語(yǔ)句
???* 返回值:查詢(xún)返回的結(jié)果集
???*/
???public static ResultSet executeQuery(String sql) {
??????? try {
??????????? if (conn == null) {
??????????????? new Dao();
??????????? }
??????????? //下面一行調(diào)用了Statement類(lèi)的executeQuery(String sql)方法
??????????? //執(zhí)行給定的 SQL 語(yǔ)句,該語(yǔ)句返回單個(gè) ResultSet 對(duì)象,絕大多數(shù)是用SELECT語(yǔ)句
??????????? returnconn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE).executeQuery(sql);
??????? } catch (SQLException e) {
??????????? e.printStackTrace();
??????????? return null;
??????? } finally {
??????? }
???}
???/*數(shù)據(jù)庫(kù)更新方法
???* 方法參數(shù):sql更新語(yǔ)句
???* 返回值:一個(gè)整數(shù),指示受影響的行數(shù)(即更新計(jì)數(shù))
???*/
???static int executeUpdate(String sql) {
?
??????? try {
??????????? if (conn == null) {
??????????????? new Dao();
??????????? }
??????????? //下面一行調(diào)用了Statement類(lèi)中的executeUpdate方法
??????????? //用于執(zhí)行 INSERT、UPDATE 或 DELETE 語(yǔ)句以及 SQL DDL(數(shù)據(jù)定義語(yǔ)言)語(yǔ)句
??????????? returnconn.createStatement().executeUpdate(sql);
??????? } catch (SQLException e) {
??????????? System.out.println(e.getMessage());
??????????? return -1;
??????? } finally {
??????? }
}
2.構(gòu)造方法
package com.dao;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
?*
?*@author Administrator
?*/
?publicclass Dao_check {
???private static String Username;
??? privatestatic String ID;
???private static String Title;
???private static String Content;
???private static String Memotype;
???private static String Memotime;
???
???public static String getUsername()
??? {
?????????????????? returnUsername;
??? }
???public static void setUsername(String Username)
??? {
?????????????????? Dao_check.Username= Username;
??? }
???public static String getID()
??? {
?????????????????? returnID;
??? }
???public static void setID(String ID)
??? {
?????????????????? Dao_check.ID= ID;
??? }
???public static String getTitle()
??? {
?????????????????? returnTitle;
??? }
???public static void setTitle(String Title)
??? {
?????????????????? Dao_check.Title= Title;
??? }
???public static String getContent()
??? {
?????????????????? returnContent;
??? }
???public static void setContent(String Content)
??? {
?????????????????? Dao_check.Content= Content;
??? }
???public static String getMemotype()
??? {
?????????????????? returnMemotype;
??? }
???public static void setMemotype(String Memotype)
??? {
?????????????????? Dao_check.Memotype= Memotype;
??? }
???public static String getMemotime()
??? {
?????????????????? returnMemotime;
??? }
???public static void setMemotime(String Memotime)
??? {
?????????????????? Dao_check.Memotime=Memotime;
??? }
???
???public static int rownum()
??? {
???????int i=0;
???????try
???????{?
???????????Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
???????? }
???????catch(ClassNotFoundException e)
???????{??
???????????System.exit(0);
???????? }
???????Connection? con=null;
???????Statement stmt=null;
???????ResultSet?? rs=null;
?
???????try
???????{?
?????????????con=DriverManager.getConnection("jdbc:sqlserver://localhost:1434;DatabaseName=XSML","sa","123456");
????????????stmt=con.createStatement();
???????????if(Username==null)
???????????{??
??????????????? Username="";
??????????????
???????????}
???????????
???????????if(Title==null)
???????????{??
??????????????? Title="";
???????????????
???????????}
??????????
???????????if(Content==null)
???????????{
??????????????? Content="";
???????????????
???????????}
???????????
???????????if(Memotype==null)
???????????{
???????????????Memotype="";
???????????????
???????????}
???????????
???????????if(Memotime==null)
???????????{
??????????????? Memotime="";
??????????????
???????????}
??????????
??????????????rs=stmt.executeQuery("SELECT *?FROM Table_1 where Username like '"+Username+"%' "
??????????????????? + "and Title like'"+Title+"%' and Content like '"+Content+"%' and Memotypelike '"+Memotype+"%' "
??????????????????? + "and Memotime like'"+Memotime+"%'");
???????????
???????????while(rs.next())
???????????{
???????????????
??????????????i=i+1;
??????????????
???????????}
???????????return i;
???????}
???????catch(SQLException e)
???????{?
???????????//e.printStackTrace();?
???????????System.exit(0);
???????? }finally{
???????????try {
??????????????? rs.close();
???????????} catch (SQLException ex) {
???????????????Logger.getLogger(Dao_check.class.getName()).log(Level.SEVERE, null, ex);
???????????}
???????????try {
??????????????? stmt.close();
???????????} catch (SQLException ex) {
???????????????Logger.getLogger(Dao_check.class.getName()).log(Level.SEVERE,null, ex);
???????????}
???????????try {
??????????????? con.close();
???????????} catch (SQLException ex) {
???????????????Logger.getLogger(Dao_check.class.getName()).log(Level.SEVERE, null, ex);
???????????}
???????}
???????return -1;
??? }
???
???
?? //在點(diǎn)擊查詢(xún)后,彈出窗口中,直接顯示第一條信息
???public static String Check_print()
??? {
???????
???????try
???????{?
???????????Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
???????? }
???????catch(ClassNotFoundException e)
???????{??
???????????System.exit(0);
???????? }
???????Connection? con=null;
???????Statement stmt=null;
???????ResultSet?? rs=null;
???????try
???????{?
?????????????con=DriverManager.getConnection("jdbc:sqlserver://localhost:1434;DatabaseName=XSML","sa","123456");
????????????stmt=con.createStatement();
???????????
//???????????System.out.println(Username);
//???????????System.out.println(Title);
//???????????System.out.println(Content);
//???????????System.out.println(Memotype);
//???????????System.out.println(Memotime);
???????????String str="1";
???????????if(Username==null)
???????????{??
??????????????? Username="";
??????????????? str=str+"+"+Username;
???????????}
???????????else str=str+"+"+Username;
???????????if(Title==null)
???????????{??
??????????????? Title="";
??????????????? str=str+"+ "+Title;
???????????}
???????????else str=str+"+"+Title;
???????????if(Content==null)
???????????{
??????????????? Content="";
???????????????str=str+"+"+Content;
???????????}
???????????else str=str+"+"+Content;
???????????if(Memotype==null)
???????????{
??????????????? Memotype="";
??????????????? str=str+"+"+Memotype;
???????????}
???????????else str=str+"+"+Memotype;
???????????if(Memotime==null)
???????????{
??????????????? Memotime="";
??????????????? str=str+"+"+Memotime+"+1";
???????????}
???????????else str=str+"+"+Memotime+"+1";
??????????????rs=stmt.executeQuery("SELECT *?FROM Table_1 where Username like '"+Username+"%' "
??????????????????? + "and Title like'"+Title+"%' and Content like '"+Content+"%' and Memotypelike '"+Memotype+"%' "
??????????????????? + "and Memotime like'"+Memotime+"%'");
???????????
??????????
??????????????? rs.next();
???????????
????? ??????Username=rs.getString("Username");????????????
???????????Title=rs.getString("Title");
???????????Content=rs.getString("Content");
???????????Memotype=rs.getString("Memotype");
???????????Memotime=rs.getString("Memotime");
???????????
//???????????System.out.println(Username);
//???????????System.out.println(Title);
//???????????System.out.println(Content);
//???????????System.out.println(Memotype);
//???????????System.out.println(Memotime);
???????????
???????????
???????????return str;
???????}
???????catch(SQLException e)
???????{?
???????????//e.printStackTrace();?
???????????System.exit(0);
???????? }finally{
???????????try {
??????????????? rs.close();
???????????} catch (SQLException ex) {
??????????????? Logger.getLogger(Dao_check.class.getName()).log(Level.SEVERE,null, ex);
???????????}
???????????try {
??????????????? stmt.close();
???????????} catch (SQLException ex) {
???????????????Logger.getLogger(Dao_check.class.getName()).log(Level.SEVERE, null, ex);
???????????}
? ??????????try {
??????????????? con.close();
???????????} catch (SQLException ex) {
???????????????Logger.getLogger(Dao_check.class.getName()).log(Level.SEVERE, null, ex);
???????????}
???????}
???????return "##";
??? }
???
???
???public static int Check_next(int?i,String str)
??? {
???????
???????try
???????{?
???????????Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
???????? }
???????catch(ClassNotFoundException e)
???????{??
???????????System.exit(0);
???????? }
???????Connection? con=null;
???????Statement stmt=null;
???????ResultSet?? rs=null;
???????try
???????{?
?????????????con=DriverManager.getConnection("jdbc:sqlserver://localhost:1434;DatabaseName=XSML","sa","123456");
????????????stmt=con.createStatement();
???????????
//???????????System.out.println(Username);
//???????????System.out.println(Title);
//???????????System.out.println(Content);
//???????????System.out.println(Memotype);
//???????????System.out.println(Memotime);
???????????int[] a=new int[6];
??? ????????a[0]=0;
???????????String[] ss=new String[10];
???????????ss=str.split("\\+");
???????????if(ss[1].equals(Username))
??????????????? a[1]=1;
???????????else Username="";
???????????if(ss[2].equals(Title))
??????????????? a[2]=2;
???????????else Title="";
???????????if(ss[3].equals(Content))
??????????????? a[3]=3;
???????????else Content="";
???????????if(ss[4].equals(Memotype))
??????????????? a[4]=4;
???????????else Memotype="";
???????????if(ss[5].equals(Memotime))
??????????????? a[5]=5;
???????????else Memotime="";
?????
??????????????rs=stmt.executeQuery("SELECT *?FROM Table_1 where Username like '"+Username+"%' "
??????????????????? + "and Title like'"+Title+"%' and Content like '"+Content+"%' and Memotypelike '"+Memotype+"%' "
??????????????????? + "and Memotime like'"+Memotime+"%'");
???????????
??????????for(int j=0;j<i;j++)
??????????{???
??????????????rs.next();
??????????}
???????????
???????????Username=rs.getString("Username");
???????????Title=rs.getString("Title");
???????????Content=rs.getString("Content");
???????????Memotype=rs.getString("Memotype");
???????????Memotime=rs.getString("Memotime");
???????????
//???????????System.out.println(Username);
//???????????System.out.println(Title);
//???????????System.out.println(Content);
//???????????System.out.println(Memotype);
//???????????System.out.println(Memotime);
???????????
???????????
???????????return 0;
???????}
???????catch(SQLException e)
???????{?
???????????//e.printStackTrace();?
???????????System.exit(0);
???????? }finally{
???????????try {
??????????????? rs.close();
???????????} catch (SQLException ex) {
???????????????Logger.getLogger(Dao_check.class.getName()).log(Level.SEVERE, null, ex);
???????????}
???????????try {
??????????????? stmt.close();
???????????} catch (SQLException ex) {
???????????????Logger.getLogger(Dao_check.class.getName()).log(Level.SEVERE, null, ex);
???????????}
???????????try {
??????????????? con.close();
???????????} catch (SQLException ex) {
???????????????Logger.getLogger(Dao_check.class.getName()).log(Level.SEVERE, null, ex);
???????????}
???????}
???????return -1;
??? }
???
???
???
}
3.調(diào)用方法,實(shí)現(xiàn)查詢(xún),跳轉(zhuǎn),上一頁(yè),下一頁(yè),返回,清空等功能的實(shí)現(xiàn)
//查詢(xún):
private voidjButton1_QueryActionPerformed(java.awt.event.ActionEvent evt) {??????????????????????????????????????????????
???String str = jTextField1_Username.getText();
??? inta = str.length();
???????if(a<1){
???????????System.out.println("success");
???????????JOptionPane.showMessageDialog(null,"查詢(xún)失敗,用戶(hù)不存在","",JOptionPane.INFORMATION_MESSAGE);}???????
???????else{
???????????
???????????Dao_check.setUsername(jTextField1_Username.getText());
???????????Dao_check.setMemotype(jTextField2_Memotype.getText());
???????????Dao_check.setMemotime(jTextField3_MemoTime.getText());
???????????Dao_check.setTitle(jTextField4_Title.getText());
???????????Dao_check.setContent(jTextField5_Content.getText())
;
???????????CheckedJFrame1 frame=new CheckedJFrame1();
???????????frame.setVisible(true);
???????????this.setVisible(false);}// TODO add your handling code here:
?
}
//清空??
private voidjButton2_DeleteActionPerformed(java.awt.event.ActionEvent evt) {???????????????????????????????????????????????
????? this.jTextField1_Username.setText(null);
????? this.jTextField2_Memotype.setText(null);
????? this.jTextField3_MemoTime.setText(null);
????? this.jTextField4_Title.setText(null);
// TODO add your handling codehere:
???}
//上一條
?
???private void jButton1_UpActionPerformed(java.awt.event.ActionEvent evt){???????????????????????????????????????????
??????? i=i-1;
??????? if(i<=0)
??????? {
??????????? System.out.println("這里彈個(gè)對(duì)話(huà)框 沒(méi)有上一條了!!!");
???????????JOptionPane.showMessageDialog(null,"沒(méi)有上一條了!!!","",JOptionPane.ERROR_MESSAGE);
??????????? i=i+1;//上面已經(jīng)執(zhí)行了i=i-1,這里要i=i+1,保持i最小不小于0
??????? }
??????? else
??????? {
??????????? Dao_check.Check_next(i,str);
???????????jTextField1_Username.setText(Dao_check.getUsername());
???????????jTextField4_Title.setText(Dao_check.getTitle());
???????????jTextField3_Memotime.setText(Dao_check.getMemotime());
???????????jTextField2_Memotype.setText(Dao_check.getMemotype());
???????????jTextArea1_Content.setText(Dao_check.getContent());
??????? }
下一條//
i=i+1;
??????? if(Dao_check.rownum()<i)
???????{
??????????? System.out.println("這里彈個(gè)對(duì)話(huà)框 沒(méi)有下一條了!!!");
???????????JOptionPane.showMessageDialog(null,"沒(méi)有下一條了!!!","",JOptionPane.ERROR_MESSAGE);
??????????? i=i-1;//上面已經(jīng)執(zhí)行了i=i+1,這里要i=i-1,保持i最大不超過(guò)行的個(gè)數(shù)
??????? }
??????? else
??????? {
??????????? Dao_check.Check_next(i,str);
???????????jTextField1_Username.setText(Dao_check.getUsername());
???????????jTextField4_Title.setText(Dao_check.getTitle());
???????????jTextField3_Memotime.setText(Dao_check.getMemotime());
??????????? jTextField2_Memotype.setText(Dao_check.getMemotype());
???????????jTextArea1_Content.setText(Dao_check.getContent());
??????? }
//跳轉(zhuǎn)
private voidjButton3_ReturnActionPerformed(java.awt.event.ActionEvent evt) {???????????????????????????????????????????????
??????? CheckJFrame frame=new CheckJFrame();
??????? frame.setVisible(true);
??????? this .setVisible(false);// TODO addyour handling code here:
???}??????????????
??????
5.????????????????注意事項(xiàng)
1.??????代碼編寫(xiě)規(guī)范,
2.??????實(shí)體的命名規(guī)范
3.??????代碼的正確性
4.??????包與包之間的連接
5.??????構(gòu)造方法的編寫(xiě)
6.??????數(shù)據(jù)庫(kù)的連接
7.??????方法的調(diào)用
8.??????數(shù)據(jù)庫(kù)的關(guān)閉?????????????????????????????
??????????????????????????????????
6.????????????????參考文獻(xiàn)
1.??????老師給的文檔代碼
2.??????百度代碼文檔
3.??????課本
4.??????百度搜索視頻講解
?
總結(jié)
- 上一篇: ubuntu双系统引导梅花_Win10+
- 下一篇: 语法基础(三. 类,属性,方法,方法重载