《JAVA实验报告-数据库编程.docx》由会员分享,可在线阅读,更多相关《JAVA实验报告-数据库编程.docx(20页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、优质文本JAVA实验报告班级:GOT7姓名: 鸟宝宝 学号:i got7指导老师:鲁鸣鸣实验三 数据库络编程一、实验目的 1. 了解和掌握Java根本概念和语法2了解和掌握Java图形界面编程的根本概念和用法 3. 了解和掌握Java事件编程的根本概念和用法4. 了解和掌握Java IO根本功能和用法5. 了解和掌握Java 数据库编程根本功能和用法 二、实验开发环境和工具可以在Linux或者Windows操作系统上搭建开发环境,可使用集成开发环境Eclipse,使用Java语言,工具包使用JDK1.6、1.7或1.8。三、实验内容 在实验一、二的根底上,拓展所实现的小软件,增加与数据库通信的
2、功能。要就能有查询、增加、删除、更新数据的功能。四 实验过程 要实现JAVA程序和数据库的通信,首先要熟悉数据库,MySQL的根本语法,例如建立数据库,创立修改查询删除表的操作,这些都清楚以后再来实现JAVA和数据库的连接,让JAVA程序去做上述事情,最后实现他们之间的相互响应,到达通信的目的。五 程序清单/效劳器端代码package zjq;import .*;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;
3、import java.io.*;public class MyServer public static void main(String args) try /效劳器在8000端口监听 ServerSocket ss = new ServerSocket(8000) ; System.out.println(效劳器正在8000端口监听.) ; Socket s = ss.accept() ; /接受用户名和密码 InputStream is = s.getInputStream() ; InputStreamReader isr = new InputStreamReader(is) ; B
4、ufferedReader br = new BufferedReader(isr) ; String uandpandc = br.readLine();/检验点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 o
5、sw = new OutputStreamWriter(os);PrintWriter pw = new PrintWriter(osw, true);Class.forName(org.gjt.mm.mysql.Driver);Connection cn = DriverManager.getConnection(jdbc:mysql:/127.0.0.1:3306/user,root,951003);PreparedStatement ps = cn.prepareStatement(select *from users where username =? and password =?)
6、;ps.setString(1, u);ps.setString(2, p);ResultSet rs = ps.executeQuery();switch(c)case login:if(rs.next()/发送正确信息到客户端pw.println(ok);else/发送错误信息到客户端pw.println(err); break;caseregister:if(rs.next()/发送正确信息到客户端pw.println(ok);elsepw.println(err);PreparedStatement ps2 = cn.prepareStatement(insert into users
7、 values(?,?,?);ps2.setString(1, u);ps2.setString(2, p);ps2.setString(3, 0);ps2.executeUpdate();break;case delete:if(rs.next()/发送正确信息到客户端pw.println(ok);PreparedStatement ps3 = cn.prepareStatement(delete from users where username = ?);ps3.setString(1, u);ps3.executeUpdate();elsepw.println(err);break;
8、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.setSi
9、ze(250 , 125) ;/new组件JLabel labUser = new JLabel(用户名) ;JLabel labPass = new JLabel(密码) ;JButton btnLogin = new JButton(登陆) ;JButton btnReg = new JButton(注册) ;JButton btnCancel = new JButton(注销) ;/注册事件监听btnLogin.addActionListener(this) ;btnReg.addActionListener(this) ;btnCancel.addActionListener(this
10、) ;/布置输入面板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(bt
11、nReg) ; 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(arg
12、0.getActionCommand()case 登录:try/发送用户名和密码到效劳器端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);
13、pw.println(user+%+pass+%+com);/接收效劳器发送回来确实认信息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); elseJOptionP
14、ane.showMessageDialog(null, 用户名或密码出错, 登录失败 , JOptionPane.ERROR_MESSAGE);catch(Exception e)e.printStackTrace() ; break;case 注册:try/发送用户名和密码到效劳器端String user = txtUser.getText();String pass = txtPass.getText();String com = register;Socket s = new Socket(localhost,8000);OutputStream os = s.getOutputStre
15、am();OutputStreamWriter osw = new OutputStreamWriter(os);PrintWriter pw = new PrintWriter(osw, true);pw.println(user+%+pass+%+com);/接收效劳器发送回来确实认信息InputStream is = s.getInputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader br = new BufferedReader(isr);String yorn = br.readLin
16、e();/显示主窗体if(yorn.equals(ok)JOptionPane.showMessageDialog(null, 用户已经注册!, 失败 , JOptionPane.ERROR_MESSAGE);elseJOptionPane.showMessageDialog(null, 用户注册成功!, 成功 , JOptionPane.ERROR_MESSAGE);catch(Exception e1)e1.printStackTrace() ;break;case 注销:try/发送用户名和密码到效劳器端String user = txtUser.getText();String pas
17、s = 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);/接收效劳器发送回来确实认信息InputStream is = s.getInputStream();Input
18、StreamReader 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
19、);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.ev
20、ent.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 = tru
21、e, 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 JBut
22、ton(蓝队先下); 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 .setT
23、ext(游戏已结束,请重新开始); 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(); re
24、paint(); 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.addActionListener(new ActionLis
25、tener() public void actionPerformed(ActionEvent arg0) for (int i = 0; i s.length; i+) si = 0; jji = 0; fa = true; repaint(); yin = false; huatu = false; jl.setForeground(Color.black); jl .setText(蓝队先下); ); setSize(500, 500); setVisible(true); public void paint(Graphics g) if (fa) super.paint(g); int
26、 x = 100, y = 100, c = 300, k = 300; for (int i = 0; i 300; i += 100) g.drawRect(x, y + i, c, k - i); for (int i = 0; i = 100 & x = 100 & y = 200 & x = 100 & y = 300 & x = 100 & y = 100 & x = 200 & y = 200 & x = 200 & y = 300 & x = 200 & y = 100 & x = 300 & y = 200 & x = 300 & y = 300 & x = 300 & y
27、400) a = 9; else a = 0; return a; public void dd() if (jj1 = 1 & jj2 = 1 & jj3 = 1) jl.setForeground(Color.RED); jl.setText(红方获胜,连接线1,2,3); yin = true; if (jj4 = 1 & jj5 = 1 & jj6 = 1) jl.setForeground(Color.RED); jl .setText(红方获胜,连接线4,5,6); yin = true; if (jj7 = 1 & jj8 = 1 & jj9 = 1) jl.setForegro
28、und(Color.RED); jl.setText(红方获胜,连接线7,8,9); yin = true; if (jj1 = 1 & jj5 = 1 & jj9 = 1) jl.setForeground(Color.RED); jl.setText(红方获获胜,连接线1,5,9); yin = true; if (jj3 = 1 & jj5 = 1 & jj7 = 1) jl.setForeground(Color.RED); jl.setText(红方获胜,连接线3,5,7); yin = true; if (jj1 = 1 & jj4 = 1 & jj7 = 1) jl.setFor
29、eground(Color.RED); jl.setText(红方获胜,连接线1,4,7); yin = true; if (jj2 = 1 & jj5 = 1 & jj8 = 1) jl.setForeground(Color.RED); jl.setText(红方获胜,连接线2,5,8); yin = true; if (jj3 = 1 & jj6 = 1 & jj9 = 1) jl.setForeground(Color.RED); jl.setText(红方获胜,连接线3,6,9); yin = true; if (jj1 = 2 & jj2 = 2 & jj3 = 2) jl.set
30、Foreground(Color.RED); jl.setText(蓝方获胜,连接线1,2,3); yin = true; if (jj4 = 2 & jj5 = 2 & jj6 = 2) jl.setForeground(Color.RED); jl.setText(蓝方获胜,连接线4,5,6); yin = true; if (jj7 = 2 & jj8 = 2 & jj9 = 2) jl.setForeground(Color.RED); jl.setText(蓝方获胜,连接线7,8,9); yin = true; if (jj1 = 2 & jj5 = 2 & jj9 = 2) jl.
31、setForeground(Color.RED); jl.setText( 蓝方获胜,连接线1,5,9 ); yin = true; if (jj3 = 2 & jj5 = 2 & jj7 = 2) jl.setForeground(Color.RED); jl.setText(蓝方获胜,连接线3,5,7 ); yin = true; if (jj1 = 2 & jj4 = 2 & jj7 = 2) jl.setForeground(Color.RED); jl.setText(蓝方获胜,连接线1,4,7 ); yin = true; if (jj2 = 2 & jj5 = 2 & jj8 = 2) jl.setForeground(Color.RED); jl.setText(蓝方获胜,连接线2,5,8); yin = true; if (jj3