已阅读5页,还剩20页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
記事小系統实验目的:1. 掌握JDBC;2. 掌握DAO模式应用。3. 对sql数据库的表的增删改查4. 实现登录操作5. 记事并写入sql表内6. 对所记内容进行增删改查实验要求:1. 数据库 ignore_love表 admin( id,name,password)表 note (标题 内容 日期)2. 创建数据库工具类adminDao定义实体类admin,与表admin 对应定义adminDao 接口创建adminDao的实现类,实现查询方法实验步骤:创建表use ignore_love;create table admin( Id int IDENTITY(1,1) NOT NULL, Name varchar(12), Password varchar(20), PRIMARY KEY (Id) );create table note( id int IDENTITY(1,1) NOT NULL, 标题 varchar(100), 内容 varchar(500), 日期 varchar(100), PRIMARY KEY (Id) );创建Dao类:package ignore_love.sql;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class BDao private static String driver = com.microsoft.sqlserver.jdbc.SQLServerDriver;static String url = jdbc:sqlserver:/127.0.0.1:1433;DatabaseName=ignore_love;static String user = sa;static String password = 123456789;static Connection conn = null; /* * 获取数据库连接对象。 */ public Connection getConnection() if(conn=null) / 获取连接并捕获异常 try Class.forName(driver); conn = DriverManager.getConnection(url, user, password); catch (Exception e) e.printStackTrace();/ 异常处理 return conn;/ 返回连接对象 /* * 关闭数据库连接。 * param conn 数据库连接 * param pstmt Statement对象 * param rs 结果集 */ public void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) / 若结果集对象不为空,则关闭 if (rs != null) try catch (Exception e) e.printStackTrace(); / 若Statement对象不为空,则关闭 if (pstmt != null) try pstmt.close(); catch (Exception e) e.printStackTrace(); / 若数据库连接对象不为空,则关闭 if (conn != null) try catch (Exception e) e.printStackTrace(); /* * * 增、删、改的操作 * param sql 预编译的 SQL 语句 * param param 预编译的 SQL 语句中的?参数的字符串数组 * return 影响的行数 */ public int exceuteUpdate(String preparedSql, Object param) PreparedStatement pstmt = null; int num = 0; conn = getConnection(); try pstmt = conn.prepareStatement(preparedSql); if (param != null) for (int i = 0; i param.length; i+) pstmt.setObject(i + 1, param); / 为预编译sql设置参数 num = pstmt.executeUpdate(); catch (SQLException e) e.printStackTrace(); finally closeAll(conn, pstmt, null); return num; package ignore_love.sql;public class admin private Integer id;private String loginId;private String password;private String 标题;private String 内容;private String 日期;public admin() public admin(String loginId, String password) this.loginId = loginId;this.password = password;public admin( String 标题, String 内容, String 日期) this.标题 = 标题;this.内容 = 内容;this.日期 = 日期;public Integer getId() return id;public void setId(Integer id) this.id = id;public String getLoginId() return loginId;public void setLoginId(String loginId) this.loginId = loginId;public String getPassword() return password;public void setPassword(String password) this.password = password;public String get标题() return 标题;public void set标题(String 标题) this.标题 = 标题;public String get内容() return 内容;public void set内容(String 内容) this.内容 = 内容;public String get日期() return 日期;public void set日期(String 日期) this.日期 = 日期;界面:登录界面:package ignore_love.activity;import ignore_love.sql.admin;import ignore_love.sql.adminDao;import ignore_love.sql.adminDaoServiceData;import java.awt.Color;import java.awt.Container;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField;import javax.swing.SwingConstants;public class admin_user extends JFrame admin admin=null; /主人private JTextField username; / 密码 private JPasswordField password; / 小容器 private JLabel title; private JLabel name; private JLabel pass; private JButton login; public admin_user() / 设置窗口标题 this.setTitle(用户登录系统); / 窗体组件初始化 init(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); / 设置布局方式为绝对定位 getContentPane().setLayout(null); this.setBounds(0, 0, 355, 265); / 窗体大小不能改变 this.setResizable(false); / 居中显示 this.setLocationRelativeTo(null); / 窗体可见 this.setVisible(true); public void init() / 创建一个容器 Container con = this.getContentPane(); title = new JLabel(笔记管理); title.setHorizontalAlignment(SwingConstants.CENTER); title.setBounds(135, 33, 80, 40); name = new JLabel(账号:); name.setBounds(62, 88, 70, 20); / 用户号码登录输入框 username = new JTextField(); username.setBounds(100, 90, 150, 20); / 密码输入框 pass = new JLabel(密码:); pass.setBounds(62, 120, 70, 20); password = new JPasswordField(); password.setBounds(100, 120, 150, 20); / 按钮设定 login = new JButton(登录); login.setFont(new Font(幼圆, Font.PLAIN, 16); login.setForeground(Color.BLACK); login.setBackground(Color.LIGHT_GRAY); login.setFocusPainted(false); login.setBounds(135, 160, 80, 40); /给按钮添加1个事件 login.addActionListener(new ActionListener() Override public void actionPerformed(ActionEvent e) String str=e.getActionCommand(); if(登录.equals(str) String getName =username.getText(); String getPwd =password.getText(); admin mas=new admin(); mas.setLoginId(getName); mas.setPassword(getPwd); adminDao masterDao = new adminDaoServiceData(); admin = masterDao.finadmin(mas); if (admin!=null) N_activity frame = new N_activity(); frame.setVisible(true); dispose(); / 关闭当前界面 else JOptionPane.showMessageDialog(null, 账号或密码有误!, 警告!,JOptionPane.ERROR_MESSAGE); ); / 所有组件用容器装载 con.add(username); con.add(password); con.add(name); con.add(pass); con.add(login); con.add(title); con.add(username); con.add(password);public static void main(String args) / TODO Auto-generated method stubadmin_user a = new admin_user();主界面:package ignore_love.activity;import ignore_love.admin.find;import ignore_love.admin.write;import ignore_love.admin.updata;import ignore_love.admin.delete;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.Font;import java.awt.Color;import java.awt.Image;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import javax.swing.ImageIcon;import javax.swing.SwingConstants;import javax.swing.JTextField;public class N_activity extends JFrame private static final long serialVersionUID = 1L;private JPanel contentPane;public N_activity() Image image = new ImageIcon(./image/background.jpg).getImage(); this.setIconImage(image);setBackground(Color.DARK_GRAY);setFont(new Font(华文楷体, Font.PLAIN, 16);setBounds(200, 200, 600, 360);setTitle(笔记管理系统);setUndecorated(true);/去除边框/居中this.setLocationRelativeTo(null);contentPane = new JPanel();contentPane.setLayout(null); contentPane.setBackground(Color.LIGHT_GRAY);contentPane.setBorder(null);setContentPane(contentPane);contentPane.setLayout(null);JButton title = new JButton(记事管理);title.setBackground(Color.DARK_GRAY);title.setHorizontalAlignment(SwingConstants.CENTER);title.setForeground(Color.WHITE);title.setFocusPainted(false);title.setFont(new Font(幼圆, Font.PLAIN, 24);title.setBounds(0, 0, 169, 160);contentPane.add(title);/退出JButton exout = new JButton(u00D7);exout.setFont(new Font(Rockwell Condensed, Font.PLAIN, 9);exout.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) System.exit(0););exout.setBackground(Color.LIGHT_GRAY);exout.setForeground(Color.RED);exout.setFocusPainted(false);exout.setBounds(560, 0, 40, 40);contentPane.add(exout);/写入ButtonJButton write = new JButton(记事);write.setFont(new Font(幼圆, Font.PLAIN, 16);write.setForeground(Color.GREEN);write.setBackground(Color.DARK_GRAY);write.setFocusPainted(false);write.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) write w = new write(new JPanel();dispose(););write.setBounds(0, 160, 169, 50);contentPane.add(write);/查询JButton find = new JButton(查看);find.setFont(new Font(幼圆, Font.PLAIN, 16);find.setForeground(Color.BLUE);find.setBackground(Color.DARK_GRAY);find.setFocusPainted(false);find.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) find f = new find(new JPanel();dispose(););find.setBounds(0, 210, 169, 50);contentPane.add(find);/删除deteleJButton detele = new JButton(删除);detele.setFont(new Font(幼圆, Font.PLAIN, 16);detele.setForeground(Color.RED);detele.setBackground(Color.DARK_GRAY);detele.setFocusPainted(false);detele.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) delete de = new delete(new JPanel();dispose(););detele.setBounds(0, 260, 169, 50);contentPane.add(detele);/更改JButton updata = new JButton(更改);updata.setFont(new Font(幼圆, Font.PLAIN, 16);updata.setForeground(Color.MAGENTA);updata.setBackground(Color.DARK_GRAY);updata.setFocusPainted(false);updata.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) updata up = new updata(new JPanel();dispose(););updata.setBounds(0, 310, 169, 50);contentPane.add(updata);/Panel writeJPanel panel = new JPanel();panel.setBackground(Color.LIGHT_GRAY);panel.setForeground(Color.DARK_GRAY);panel.setBounds(167, 0, 433, 360);panel.setLayout(null);contentPane.add(panel);JLabel Pt = new JLabel(记事管理);Pt.setHorizontalAlignment(SwingConstants.CENTER);Pt.setFont(new Font(幼圆, Font.PLAIN, 40);Pt.setForeground(Color.BLACK);Pt.setBackground(Color.DARK_GRAY);Pt.setBounds(0, 118, 433, 55);panel.add(Pt);Date date=new Date();DateFormat format=new SimpleDateFormat(yyyy-MM-dd);String time=format.format(date);JLabel ViewTime = new JLabel(time);ViewTime.setForeground(Color.RED);ViewTime.setVerticalAlignment(SwingConstants.BOTTOM);ViewTime.setHorizontalAlignment(SwingConstants.RIGHT);ViewTime.setFont(new Font(宋体, Font.PLAIN, 20);ViewTime.setBounds(207, 295, 203, 55);panel.add(ViewTime);Write界面:package ignore_love.admin;import ignore_love.activity.N_activity;import ignore_love.sql.BDao;import java.awt.Color;import java.awt.Font;import java.awt.TextArea;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;public class write extends JFrame public write(JPanel p) setUndecorated(true);/消除JFrame边框this.setBackground(Color.LIGHT_GRAY);this.setForeground(Color.DARK_GRAY);this.setBounds(167, 0, 433, 389);getContentPane().setLayout(null);/居中this.setLocationRelativeTo(null);JTextField Pt = new JTextField(记事);Pt.setHorizontalAlignment(SwingConstants.CENTER);Pt.setFont(new Font(幼圆, Font.PLAIN, 40);Pt.setForeground(Color.GREEN);Pt.setBackground(Color.WHITE);Pt.setEditable(false);Pt.setBounds(83, 10, 260, 45);getContentPane().add(Pt);Date date=new Date();DateFormat format=new SimpleDateFormat(yyyy-MM-dd HH:mm);String time=format.format(date);JTextField ViewTime = new JTextField(time);ViewTime.setHorizontalAlignment(SwingConstants.CENTER);ViewTime.setFont(new Font(宋体, Font.PLAIN, 20);ViewTime.setBounds(83, 90, 260, 30);ViewTime.setEditable(false);getContentPane().add(ViewTime);ViewTime.setColumns(10);JTextField Jtitle = new JTextField(u8F93u5165u6807u9898);Jtitle.setForeground(Color.BLUE);Jtitle.setToolTipText();Jtitle.setHorizontalAlignment(SwingConstants.LEFT);Jtitle.setFont(new Font(宋体, Font.PLAIN, 20);Jtitle.setBounds(83, 130, 260, 30);getContentPane().add(Jtitle);Jtitle.setColumns(10);TextArea Jauto = new TextArea(输入事件);Jauto.setForeground(Color.BLUE);Jauto.setFont(new Font(宋体, Font.PLAIN, 15);Jauto.setBounds(83, 170, 260, 141);getContentPane().add(Jauto);/提交ButtonJButton write = new JButton(完成);write.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String driver = com.microsoft.sqlserver.jdbc.SQLServerDriver; String url = jdbc:sqlserver:/127.0.0.1:1433;DatabaseName=ignore_love;String user = sa;String password = 123456789;Connection conn = null;try Class.forName(driver);conn = DriverManager.getConnection(url, user, password);String getTitle =Jtitle.getText();String getAuto=Jauto.getText();Statement stmt = conn.createStatement();String strSql = insert into note (标题,内容,日期) values(+getTitle+,+getAuto+,+time+);stmt.executeUpdate(strSql);System.out.println(成功);if(null != conn)conn.close(); catch (ClassNotFoundException e1) System.out.println(未写入!);e1.printStackTrace();catch (SQLException e1) e1.printStackTrace();N_activity frame = new N_activity();frame.setVisible(true);dispose(); / 关闭当前界面);write.setFont(new Font(幼圆, Font.PLAIN, 16);write.setForeground(Color.BLACK);write.setBackground(Color.LIGHT_GRAY);write.setFocusPainted(false);write.setBounds(250, 317, 93, 33);getContentPane().add(write);/返回ButtonJButton canel = new JButton(返回);canel.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) N_activity frame = new N_activity();frame.setVisible(true);dispose(); / 关闭当前界面);canel.setFont(new Font(幼圆, Font.PLAIN, 16);canel.setForeground(Color.BLACK);canel.setBackground(Color.LIGHT_GRAY);canel.setFocusPainted(false);canel.setBounds(83, 317, 93, 33);getContentPane().add(canel);this.setVisible(true);Find界面:package ignore_love.admin;import ignore_love.activity.N_activity;import ignore_love.sql.BDao;import java.awt.Color;import java.awt.Font;import java.awt.TextArea;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.JButton;import javax.swing.table.JTableHeader;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;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.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;public class find extends JFrame private JScrollPane scpDemo;private JTableHeader jth;private JTable tabDemo;public find(JPanel p) setUndecorated(true);/消除JFrame边框this.setBackground(Color.LIGHT_GRAY);this.setForeground(Color.DA
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 吉林省长春市第八十九中学2025-2026学年七年级上学期10月月考道德与法治试题(含答案)
- 长沙高考语文试题及答案
- 2025年家居建材行业绿色环保与智能家居产品研究报告及未来发展趋势预测
- 财务管理多维度分析模板
- 读小王子有感对友谊与成长的理解读后感8篇范文
- 2025年牧师技能测试题目及答案
- 售后服务品质保障承诺函(3篇)
- 安全员证考题题库直播及答案解析
- 2025年数字货币行业数字货币法律法规解读报告
- 2025年物流科技行业智能物流解决方案研究报告及未来发展趋势预测
- 银行消防安全培训资料
- 检查井有限空间施工专项方案
- 调相机本体安装施工方案
- 2025军队文职电子版资料公共科目真题及答案
- 社区工会创意活动方案
- 2025年幼儿园教师高级职称业务考试(幼教知识)冲刺模拟试题及答案
- 安全生产考核评分细则
- 模具企业员工管理手册样本
- 中国数学知识典故
- 2025重庆辅警考试笔试必刷题
- 财务报表审计流程模板精准审查版
评论
0/150
提交评论