JAVA實(shí)驗(yàn)報告-數(shù)據(jù)庫編程.docx_第1頁
JAVA實(shí)驗(yàn)報告-數(shù)據(jù)庫編程.docx_第2頁
JAVA實(shí)驗(yàn)報告-數(shù)據(jù)庫編程.docx_第3頁
JAVA實(shí)驗(yàn)報告-數(shù)據(jù)庫編程.docx_第4頁
JAVA實(shí)驗(yàn)報告-數(shù)據(jù)庫編程.docx_第5頁
已閱讀5頁,還剩27頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

JAVA實(shí)驗(yàn)報告班級:GOT7姓名: 鳥寶寶 學(xué)號:i got7指導(dǎo)老師:魯鳴鳴實(shí)驗(yàn)三 數(shù)據(jù)庫絡(luò)編程一、實(shí)驗(yàn)?zāi)康?1. 了解和掌握J(rèn)ava基本概念和語法2了解和掌握J(rèn)ava圖形界面編程的基本概念和用法 3. 了解和掌握J(rèn)ava事件編程的基本概念和用法4. 了解和掌握J(rèn)ava IO基本功能和用法5. 了解和掌握J(rèn)ava 數(shù)據(jù)庫編程基本功能和用法 二、實(shí)驗(yàn)開發(fā)環(huán)境和工具可以在Linux或者Windows操作系統(tǒng)上搭建開發(fā)環(huán)境,可使用集成開發(fā)環(huán)境Eclipse,使用Java語言,工具包使用JDK1.6、1.7或1.8。三、實(shí)驗(yàn)內(nèi)容 在實(shí)驗(yàn)一、二的基礎(chǔ)上,拓展所實(shí)現(xiàn)的小軟件,增加與數(shù)據(jù)庫通信的功能。要就能有查詢、增加、刪除、更新數(shù)據(jù)的功能。4 實(shí)驗(yàn)過程 要實(shí)現(xiàn)JAVA程序和數(shù)據(jù)庫的通信,首先要熟悉數(shù)據(jù)庫,MySQL的基本語法,例如建立數(shù)據(jù)庫,創(chuàng)建修改查詢刪除表的操作,這些都清楚以后再來實(shí)現(xiàn)JAVA和數(shù)據(jù)庫的連接,讓JAVA程序去做上述事情,最后實(shí)現(xiàn)他們之間的相互響應(yīng),達(dá)到通信的目的。5 程序清單/服務(wù)器端代碼package zjq;import .*;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.io.*;public class MyServer public static void main(String args) try /服務(wù)器在8000端口監(jiān)聽 ServerSocket ss = new ServerSocket(8000) ; System.out.println(服務(wù)器正在8000端口監(jiān)聽.) ; Socket s = ss.accept() ; /接受用戶名和密碼 InputStream is = s.getInputStream() ; InputStreamReader isr = new InputStreamReader(is) ; BufferedReader br = new BufferedReader(isr) ; String uandpandc = br.readLine();/檢驗(yàn)點(diǎn)System.out.println(uandpandc);String u = ;String p = ;String c = ;tryu = uandpandc.split(%)0;p = uandpandc.split(%)1;c = uandpandc.split(%)2;catch(Exception ee)OutputStream os = s.getOutputStream();OutputStreamWriter osw = new OutputStreamWriter(os);PrintWriter pw = new PrintWriter(osw, true);Class.forName(org.gjt.mm.mysql.Driver);Connection cn = DriverManager.getConnection(jdbc:mysql:/:3306/user,root,951003);PreparedStatement ps = cn.prepareStatement(select *from users where username =? and password =?);ps.setString(1, u);ps.setString(2, p);ResultSet rs = ps.executeQuery();switch(c)case login:if(rs.next()/發(fā)送正確信息到客戶端pw.println(ok);else/發(fā)送錯誤信息到客戶端pw.println(err); break;caseregister:if(rs.next()/發(fā)送正確信息到客戶端pw.println(ok);elsepw.println(err);PreparedStatement ps2 = cn.prepareStatement(insert into users values(?,?,?);ps2.setString(1, u);ps2.setString(2, p);ps2.setString(3, 0);ps2.executeUpdate();break;case delete:if(rs.next()/發(fā)送正確信息到客戶端pw.println(ok);PreparedStatement ps3 = cn.prepareStatement(delete from users where username = ?);ps3.setString(1, u);ps3.executeUpdate();elsepw.println(err);break; catch(Exception e) /登陸界面代碼package zjq;import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.* ;import .* ;public class login extends JFrame implements ActionListener JTextField txtUser = new JTextField() ;JPasswordField txtPass = new JPasswordField();public login() this.setSize(250 , 125) ;/new組件JLabel labUser = new JLabel(用戶名) ;JLabel labPass = new JLabel(密碼) ;JButton btnLogin = new JButton(登陸) ;JButton btnReg = new JButton(注冊) ;JButton btnCancel = new JButton(注銷) ;/注冊事件監(jiān)聽btnLogin.addActionListener(this) ;btnReg.addActionListener(this) ;btnCancel.addActionListener(this) ;/布置輸入面板JPanel panInput = new JPanel() ;panInput.setLayout(new GridLayout(2 , 2) ;panInput.add(labUser) ;panInput.add(txtUser) ;panInput.add(labPass) ;panInput.add(txtPass) ; /布置按鈕面板 JPanel panButton = new JPanel() ; panButton.setLayout(new FlowLayout() ; panButton.add(btnLogin) ; panButton.add(btnReg) ; panButton.add(btnCancel) ;/布置窗體 this.setLayout(new BorderLayout() ; this.add(panInput , BorderLayout.CENTER) ; this.add(panButton , BorderLayout.SOUTH) ; public static void main(String args) login w = new login() ; w.setVisible(true) ; public void actionPerformed(ActionEvent arg0) switch(arg0.getActionCommand()case 登錄:try/發(fā)送用戶名和密碼到服務(wù)器端String user = txtUser.getText();String pass = txtPass.getText();String com = login;Socket s = new Socket(localhost,8000);OutputStream os = s.getOutputStream();OutputStreamWriter osw = new OutputStreamWriter(os);PrintWriter pw = new PrintWriter(osw, true);pw.println(user+%+pass+%+com);/接收服務(wù)器發(fā)送回來的確認(rèn)信息InputStream is = s.getInputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader br = new BufferedReader(isr);String yorn = br.readLine();/顯示主窗體if(yorn.equals(ok)zjt m=new zjt(); m.setVisible(true); this.setVisible(false); elseJOptionPane.showMessageDialog(null, 用戶名或密碼出錯, 登錄失敗 , JOptionPane.ERROR_MESSAGE);catch(Exception e)e.printStackTrace() ; break;case 注冊:try/發(fā)送用戶名和密碼到服務(wù)器端String user = txtUser.getText();String pass = txtPass.getText();String com = register;Socket s = new Socket(localhost,8000);OutputStream os = s.getOutputStream();OutputStreamWriter osw = new OutputStreamWriter(os);PrintWriter pw = new PrintWriter(osw, true);pw.println(user+%+pass+%+com);/接收服務(wù)器發(fā)送回來的確認(rèn)信息InputStream is = s.getInputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader br = new BufferedReader(isr);String yorn = br.readLine();/顯示主窗體if(yorn.equals(ok)JOptionPane.showMessageDialog(null, 用戶已經(jīng)注冊!, 失敗 , JOptionPane.ERROR_MESSAGE);elseJOptionPane.showMessageDialog(null, 用戶注冊成功!, 成功 , JOptionPane.ERROR_MESSAGE);catch(Exception e1)e1.printStackTrace() ;break;case 注銷:try/發(fā)送用戶名和密碼到服務(wù)器端String user = txtUser.getText();String pass = txtPass.getText();String com = delete;Socket s = new Socket(localhost,8000);OutputStream os = s.getOutputStream();OutputStreamWriter osw = new OutputStreamWriter(os);PrintWriter pw = new PrintWriter(osw, true);pw.println(user+%+pass+%+com);/接收服務(wù)器發(fā)送回來的確認(rèn)信息InputStream is = s.getInputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader br = new BufferedReader(isr);String yorn = br.readLine();/顯示主窗體if(yorn.equals(ok)JOptionPane.showMessageDialog(null, 該用戶已注銷!, 注銷成功 , JOptionPane.ERROR_MESSAGE);elseJOptionPane.showMessageDialog(null, 該用戶不存在, 注銷失敗 , JOptionPane.ERROR_MESSAGE);catch(Exception e1)e1.printStackTrace() ;break;default:break;/游戲界面代碼package zjq;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import .Socket;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;public class zjt extends JFrame int x = 0, y = 0;int a = 0;int x1 = 0, y1 = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0, x4 = 0, y4 = 0;boolean fa = true, huatu = true, yin = false; int a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0, a7 = 0, a8 = 0, a9 = 0; int s = new int10, jj = new int10; JButton congxin, huanse; JLabel jl; public zjt() Container c = getContentPane(); c.setLayout(new FlowLayout(); congxin = new JButton(紅隊先下); c.add(congxin); huanse = new JButton(藍(lán)隊先下); c.add(huanse); jl = new JLabel(); c.add(jl, BorderLayout.SOUTH); addMouseListener(new MouseAdapter() public void mousePressed(MouseEvent event) if (event.isAltDown() repaint(); x = event.getPoint().x; y = event.getPoint().y; int a = mm(x, y); if (yin) jl.setForeground(Color.RED); jl .setText(游戲已結(jié)束,請重新開始); else if (sa = 1) jl.setForeground(Color.RED); jl.setText( 此位置有棋子,請另選位置重新下棋 ); else fa = false; if (a = 1) a1 = 1; if (a = 2) a2 = 1; if (a = 3) a3 = 1; if (a = 4) a4 = 1; if (a = 5) a5 = 1; if (a = 6) a6 = 1; if (a = 7) a7 = 1; if (a = 8) a8 = 1; if (a = 9) a9 = 1; jl.setText(); repaint(); sa = 1; );congxin.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) for (int i = 0; i s.length; i+) si = 0; jji = 0; fa = true; repaint(); yin = false; huatu = true; jl.setForeground(Color.black); jl.setText(紅隊先下); ); huanse.addAction

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論