java与数据库的操作(增删改查)

发布时间:2024年01月22日

?Java编写的程序不依赖于具体的数据库,通过JDBC访问数据库。JDBC(Java Data Base Connectivity)是Java程序访问数据库的标准接口,由一组类和接口组成。使用JDBC访问数据库的步骤:

(1)加载驱动程序

(2)建立连接对象

(3)创建语句对象

(4)执行SQL语句,获得结果

(5)关闭建立的对象,释放资源

1.测试数据库连接

import java.sql.*;

public class Dao { // 该类中包含了数据库连接相关的属性和方法。
?? ?String dbDriver = "com.mysql.jdbc.Driver"; // dbDriver 存储了数据库驱动程序的类名。
?? ?String uri = "jdbc:mysql://localhost:3306/z?characterEncoding=utf-8"; // 3306:mysql端口号。 z:数据库名字。
?? ?String user = "root"; // user 字段存储了数据库的用户名。
?? ?String password = ""; // password 字段存储了数据库的密码。
?? ?Connection c = null;

?? ?public Connection getConnection() throws ClassNotFoundException, SQLException {
?? ??? ?Class.forName(dbDriver); // 1.加载驱动程序
?? ??? ?c = DriverManager.getConnection(uri, user, password); // 2.建立连接对象
?? ??? ?return c;
?? ?}
?? ?// 连接提醒
?? ?public static void main(String args[]) {
?? ??? ?try {
?? ??? ??? ?Class.forName("com.mysql.jdbc.Driver"); // 加载驱动程序
?? ??? ??? ?System.out.println("成功加载Mysql驱动程序!");
?? ??? ?} catch (Exception e) {
?? ??? ??? ?System.out.print("加载Mysql驱动程序时出错!");
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?try {
?? ??? ??? ?Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/z?characterEncoding=utf-8",
?? ??? ??? ??? ??? ?"root", "");
?? ??? ??? ?System.out.println("成功连接Mysql服务器!");
?? ??? ??? ?Statement stmt = connect.createStatement();
?? ??? ??? ?ResultSet rs = stmt.executeQuery("select * from guanggao");
?? ??? ??? ?while (rs.next()) {
?? ??? ??? ??? ?System.out.println(rs.getString("序号"));
?? ??? ??? ?}
?? ??? ??? ?connect.close();
?? ??? ?} catch (Exception e) {
?? ??? ??? ?System.out.print("获取数据错误!");
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}

}

2.以下以广告表为例,创建广告表类:

public class guanggao {
String id;
String name;
String content;
String check;
String time;
?? ?String point;
public guanggao(String s1, String name, String content, String check, String s2, String s3) {
?? ?super();
?? ?this.id = s1;
?? ?this.name = name;
?? ?this.content = content;
?? ?this.check = check;
?? ?this.time = s2;
?? ?this.point = s3;
}

public guanggao(String id) {
?? ?super();
?? ?this.id = id;
?? ?
}


public guanggao(String name, String check) {
?? ?super();
?? ?this.name = name;
?? ?this.check = check;
}

public String getId() {
?? ?return id;
}
public void setId(String id) {
?? ?this.id = id;
}
public String getName() {
?? ?return name;
}
public void setName(String name) {
?? ?this.name = name;
}
public String getContent() {
?? ?return content;
}
public void setContent(String content) {
?? ?this.content = content;
}
public String getCheck() {
?? ?return check;
}
public void setCheck(String check) {
?? ?this.check = check;
}
public String getTime() {
?? ?return time;
}
public void setTime(String time) {
?? ?this.time = time;
}
public String getPoint() {
?? ?return point;
}
public void setPoint(String point) {
?? ?this.point = point;
}
@Override
public String toString() {
?? ?return "guanggao [id=" + id + ", name=" + name + ", content=" + content + ", check=" + check + ", time=" + time
?? ??? ??? ?+ ", point=" + point + "]";
}
}
3.创建增删改方法

import java.sql.*;
import java.util.*;

import javax.swing.JOptionPane;

public class guanggao_Dao extends Dao {

?? ?public ArrayList<guanggao> search() {
?? ??? ?ArrayList<guanggao> g = new ArrayList<guanggao>();
?? ??? ?Connection c = null;
?? ??? ?PreparedStatement pst;
?? ??? ?ResultSet rs = null;
?? ??? ?try {
?? ??? ??? ?c = getConnection(); // 2.建立连接对象
?? ??? ??? ?String sql = "select * from guanggao ";
?? ??? ??? ?pst = c.prepareStatement(sql); // 3.创建语句对象
?? ??? ??? ?rs = pst.executeQuery();
?? ??? ??? ?while (rs.next()) {
?? ??? ??? ??? ?String id = rs.getString("序号");
?? ??? ??? ??? ?String name = rs.getString("广告名");
?? ??? ??? ??? ?String content = rs.getString("广告内容");
?? ??? ??? ??? ?String check = rs.getString("审核状态");
?? ??? ??? ??? ?String time = rs.getString("时间");
?? ??? ??? ??? ?String point = rs.getString("点击率");
?? ??? ??? ??? ?guanggao gua = new guanggao(id, name, content, check, time, point);
?? ??? ??? ??? ?g.add(gua); // 将对象添加到数组
?? ??? ??? ?}
?? ??? ?} catch (ClassNotFoundException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?return g;
?? ?}

?? ?public void Insert(guanggao gao) {

?? ??? ?Connection c = null;
?? ??? ?PreparedStatement pst;
//?? ??? ?ResultSet rs=null;

?? ??? ?try {
?? ??? ??? ?c = getConnection(); // 2.建立连接对象
?? ??? ??? ?String sql1 = "insert into guanggao values(?,?,?,?,?,?)";
?? ??? ??? ?pst = c.prepareStatement(sql1); // 3.创建语句对象
?? ??? ??? ?pst.setString(1, gao.getId()); // 指定第一个?的数值,
?? ??? ??? ?pst.setString(2, gao.getName()); // 指定第二个?的数值
?? ??? ??? ?pst.setString(3, gao.getContent()); // 指定第三个?的数值
?? ??? ??? ?pst.setString(4, gao.getCheck()); // 指定第四个?的数值
?? ??? ??? ?pst.setString(5, gao.getTime());
?? ??? ??? ?pst.setString(6, gao.getPoint());
?? ??? ??? ?int re = pst.executeUpdate();
?? ??? ??? ?System.out.println(re);

?? ??? ??? ?pst.close();
?? ??? ??? ?c.close();
?? ??? ??? ?loadData();
?? ??? ?} catch (ClassNotFoundException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}

?? ?public void Delete(guanggao gao) throws SQLException {
?? ??? ?Connection c = null;
?? ??? ?PreparedStatement pst;

?? ??? ?try {
?? ??? ??? ?c = getConnection();
?? ??? ??? ?String sql1 = "delete from ?guanggao where 序号= ?";
?? ??? ??? ?pst = c.prepareStatement(sql1);
?? ??? ??? ?pst.setString(1, gao.getId());

?? ??? ??? ?int a = pst.executeUpdate();
?? ??? ??? ?System.out.println(a);
?? ??? ??? ?if (a == 1) {
?? ??? ??? ??? ?JOptionPane.showMessageDialog(null, "成功");
?? ??? ??? ?}
?? ??? ??? ?pst.close();
?? ??? ??? ?c.close();

?? ??? ?} catch (ClassNotFoundException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?} // 2.建立连接对象

?? ?}

?? ?public void loadData() {

?? ??? ?Connection c = null;
?? ??? ?PreparedStatement pst;

?? ??? ?try {
?? ??? ??? ?c = getConnection(); // 2.建立连接对象
?? ??? ??? ?String sql = "UPDATE ? guanggao ?SET 点击率 = 点击率 + 1";
?? ??? ??? ?pst = c.prepareStatement(sql); // 3.创建语句对象
?? ??? ??? ?int rs = pst.executeUpdate();

?? ??? ??? ?System.out.println(rs);
?? ??? ??? ?if (rs == 1) {
?? ??? ??? ??? ?JOptionPane.showMessageDialog(null, "成功");
?? ??? ??? ?}

?? ??? ?} catch (ClassNotFoundException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}

?? ?}

?? ?public void Updata(guanggao gao) {
?? ??? ?try {
?? ??? ??? ?Connection c = null;
?? ??? ??? ?PreparedStatement pst;
?? ??? ??? ?c = getConnection(); // 2.建立连接对象
?? ??? ??? ?String sql1 = "update guanggao set 审核状态=? where 广告名=?";
?? ??? ??? ?pst = c.prepareStatement(sql1); // 3.创建语句对象
//?? ??? ?

?? ??? ??? ?pst.setString(1, gao.getCheck()); // 指定第一个?的数值,
?? ??? ??? ?pst.setString(2, gao.getName()); // 指定第二个?的数值

?? ??? ??? ?int a = pst.executeUpdate();
?? ??? ??? ?System.out.println(a);
?? ??? ??? ?if (a == 1) {
?? ??? ??? ??? ?JOptionPane.showMessageDialog(null, "成功");
?? ??? ??? ?}
?? ??? ??? ?pst.close();
?? ??? ??? ?c.close();
?? ??? ??? ?loadData();
?? ??? ?} catch (ClassNotFoundException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
?? ?public void re(guanggao gao) {
?? ??? ?ArrayList<guanggao> g = new ArrayList<guanggao>();
?? ??? ?Connection c = null;
?? ??? ?PreparedStatement pst;
?? ??? ?ResultSet rs = null;
?? ??? ?try {
?? ??? ??? ?c = getConnection(); // 2.建立连接对象
?? ??? ??? ?String sql = "select * from guanggao ";
?? ??? ??? ?pst = c.prepareStatement(sql); // 3.创建语句对象
?? ??? ??? ?rs = pst.executeQuery();
?? ??? ??? ?while (rs.next()) {
?? ??? ??? ??? ?String id = rs.getString("序号");
?? ??? ??? ??? ?String name = rs.getString("广告名");
?? ??? ??? ??? ?String content = rs.getString("广告内容");
?? ??? ??? ??? ?String check = rs.getString("审核状态");
?? ??? ??? ??? ?String time = rs.getString("时间");
?? ??? ??? ??? ?String point = rs.getString("点击率");
?? ??? ??? ??? ?guanggao gua = new guanggao(id, name, content, check, time, point);
?? ??? ??? ??? ?g.add(gua); // 将对象添加到数组
?? ??? ??? ?}
?? ??? ?} catch (ClassNotFoundException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (SQLException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?
?? ?}

}

4.创建一个简单的登陆按钮,在该类中创建主窗口并对其做出一些美化

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
//import java.sql.Connection;
//import java.sql.PreparedStatement;
//import java.sql.ResultSet;
//import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
//import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import extra.zy_Dao.Mywindow5;
import extra.zy_Dao.guanggao_Dao;
import extra.zy_Dao.Mywindow4;
import extra.zy_Dao.Mywindow3;

public class MyWindow2 extends JFrame implements ActionListener {

?? ?private JButton bt;

?? ?public MyWindow2() {
?? ??? ?init();
?? ??? ?setTitle("广告登录页面");
?? ??? ?setSize(350, 250);
?? ??? ?setVisible(true);
?? ??? ?setLocation(400, 320);
?? ??? ?setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
?? ??? ?}
?? ?public void init() {
?? ??? ?bt = new JButton("登录");
?? ??? ?add(bt);
?? ??? ?bt.addActionListener(this);
?? ??? ?}
?? ?@Override
?? ?public void actionPerformed(ActionEvent e) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?MyWindow1 mywindow = new MyWindow1();}
?? ?public static void main(String[] args) {
?? ??? ?SwingUtilities.invokeLater(new Runnable() {
?? ??? ??? ?@Override
?? ??? ??? ?public void run() {
?? ??? ??? ??? ?MyWindow2 mywindow1 = new MyWindow2();
?? ?}
?? ?});

?? ?}

}

?

class MyWindow1 extends JFrame implements ActionListener {

?? ?private JScrollPane scpDemo;
?? ?private JTableHeader jth;
?? ?private JTable tabDemo;
?? ?private JButton b, b2, b3, b4;
?? ?private TextField text1, text2, text3;
?? ?JMenuBar menubar;
?? ?JMenuItem connectitem;
?? ?JMenu databaseMenu;
?? ?JMenu menu;
?? ?JMenuItem eitem;

?? ?public void createAndShowGUI() {
?? ??? ?// 创建窗口
?? ??? ?JFrame frame = new JFrame("FlowLayout Example");

?? ??? ?// 设置窗口关闭操作
?? ??? ?frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

?? ??? ?// 创建父容器并设置为流式布局
?? ??? ?JPanel parentPanel = new JPanel();
?? ??? ?parentPanel.setLayout(new FlowLayout());
?? ??? ?parentPanel.add(b);
?? ??? ?parentPanel.add(b2);
?? ??? ?parentPanel.add(b3);
?? ??? ?parentPanel.add(b4);
?? ??? ?frame.add(parentPanel);

?? ?}

?? ?public MyWindow1() {
?? ??? ?init();
?? ??? ?setTitle("广告信息");
?? ??? ?setSize(650, 550);
?? ??? ?setVisible(true);
?? ??? ?setLocation(400, 320);
?? ??? ?setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
?? ?}

?? ?public void init() {
?? ??? ?setLayout(new BorderLayout());
?? ??? ?setLayout(new FlowLayout());
?? ??? ?this.scpDemo = new JScrollPane();
?? ??? ?scpDemo.setSize(500, 600);
?? ??? ?text1 = new TextField();
?? ??? ?text2 = new TextField();
?? ??? ?text3 = new TextField();
?? ??? ?b = new JButton("刷新");
?? ??? ?b2 = new JButton("添加数据");
?? ??? ?b3 = new JButton("修改数据");
?? ??? ?b4 = new JButton("删除数据");
?? ??? ?b2.setBounds(165, 10, 125, 30);
?? ??? ?b3.setBounds(310, 10, 125, 30);
?? ??? ?b4.setSize(30, 50);
?? ??? ?this.b.setBounds(300, 400, 225, 300);
?? ??? ?menubar = new JMenuBar();
?? ??? ?menu = new JMenu("文件");
?? ??? ?eitem = new JMenuItem("退出",new ImageIcon("pic/10.jpg"));
?? ??? ?eitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
?? ??? ?eitem.addActionListener(new ActionListener() {
?? ??? ??? ?public void actionPerformed(ActionEvent e) {
?? ??? ??? ??? ?System.exit(0);
?? ??? ??? ?}
?? ??? ?});
?? ??? ?databaseMenu = new JMenu("数据库操作");
?? ??? ?connectitem = new JMenuItem("连接",new ImageIcon("pic/00.jpg"));? ? ?
//?? ??? ?connectitem.setIcon(new ImageIcon("0.jpg"));
?? ??? ?connectitem.setAccelerator(KeyStroke.getKeyStroke("A"));
?? ??? ?connectitem.addActionListener(new ActionListener() {
?? ??? ??? ?public void actionPerformed(ActionEvent e) {

?? ??? ??? ??? ?guanggao_Dao ga1 = new guanggao_Dao();

?? ??? ??? ??? ?ArrayList<guanggao> list = new ArrayList<guanggao>();

?? ??? ??? ??? ?ArrayList<guanggao> g = ga1.search();
?? ??? ??? ??? ?list.addAll(g);

?? ??? ??? ?}
?? ??? ?});
?? ??? ?databaseMenu.add(connectitem);
?? ??? ?menu.add(menu);
?? ??? ?menu.add(eitem);
?? ??? ?menubar.add(databaseMenu);
?? ??? ?menubar.add(menu);
?? ??? ?setJMenuBar(menubar);
?? ??? ?add(b, BorderLayout.NORTH);
?? ??? ?add(b2, BorderLayout.NORTH);
?? ??? ?add(b3, BorderLayout.NORTH);
?? ??? ?add(b4, BorderLayout.NORTH);

?? ??? ?// 设置列数
?? ??? ?b.addActionListener(this);
?? ??? ?b2.addActionListener(this);
?? ??? ?b3.addActionListener(this);
?? ??? ?b4.addActionListener(this);

?? ??? ?ArrayList<guanggao> list = new ArrayList<guanggao>();
?? ??? ?guanggao_Dao ga = new guanggao_Dao();
?? ??? ?ArrayList<guanggao> g = ga.search();
?? ??? ?list.addAll(g);
?? ??? ?JTable table = new JTable();// 创建表格
?? ??? ?String[] title = { "序号", "广告名", "广告内容", "审核状态", "时间", "点击率" };// 表头信息
?? ??? ?// 创建表格模型
?? ??? ?DefaultTableModel tableModel = new DefaultTableModel(null, title);
?? ??? ?for (guanggao gao : list) {
?? ??? ??? ?Object[] rowData = { gao.getId(), gao.getName(), gao.getContent(), gao.getCheck(), gao.getTime(),
?? ??? ??? ??? ??? ?gao.getPoint() };
?? ??? ??? ?tableModel.addRow(rowData);// 把ArrayList中的数据放入表格模型
?? ??? ?}
?? ??? ?table.setModel(tableModel);
?? ??? ?// 表格间距
?? ??? ?// 美化

?? ??? ?JTableHeader header = table.getTableHeader();
?? ??? ?header.setBackground(Color.gray);
?? ??? ?header.setFont(new Font("宋体", Font.BOLD, 16));
//窗口上下栏间距
?? ??? ?table.setSize(630, 450);
?? ??? ?// 把表格添加到窗口中
?? ??? ?add(new JScrollPane(table), BorderLayout.CENTER);// 窗口的默认布局是边界布局
?? ??? ?
?? ??? ?ImageIcon icon=new ImageIcon("pic/15.jpg");//背景图。插入图片,可删除该行代码
?? ? ? ? ?JLabel label = new JLabel(icon);//往一个标签中加入图片
?? ? ? ?
?? ? ? ? ?label.setBounds(0, 0, icon.getIconWidth(),icon.getIconHeight());//设置标签位置大小为图片大小
?? ? ? ? ?this.getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE));//标签添加到第二层面板
?? ? ? ? ?
?? ? ? ? ?//获取顶层容器设为透明
?? ? ? ? ?JPanel imPanel = (JPanel)getContentPane();
?? ? ? ? ?imPanel.setOpaque(false);
?? ? ? ? ?
?? ? ? ? ?//建立透明文本显示区面板 ? ?
?? ? ? ? ?JPanel displayPanel=new JPanel();
?? ? ? ? ?displayPanel.setOpaque(false);

?? ? ? ?}

?? ?

?? ?@Override
?? ?public void actionPerformed(ActionEvent e) {

?? ??? ?if (e.getSource() == b2) {
?? ??? ??? ?Mywindow3 m3 = new Mywindow3();

?? ??? ?}

?? ??? ?else if (e.getSource() == b3) {
?? ??? ??? ?Mywindow4 m4 = new Mywindow4();
?? ??? ?} else if (e.getSource() == b4) {
?? ??? ??? ?Mywindow5 m5 = new Mywindow5();

?? ??? ?} else if (e.getSource() == b) {

?? ??? ??? ?Mywindow6 m6 = new Mywindow6();

?? ??? ?}

?? ?}

}

5.增加数据窗口

mport java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.*;

import extra.zy_Dao.guanggao_Dao;;

public class Mywindow3 extends JFrame implements ActionListener {
?? ?JTextField text, text1, text2, text3, text4, text5;
?? ?JLabel jl, jl1, jl2, jl3, jl4, jl5;
?? ?JPanel jp, jp1;
?? ?JButton b;

?? ?public Mywindow3() {
?? ??? ?init();
?? ??? ?setTitle("增加数据页面");
?? ??? ?setSize(450, 350);
?? ??? ?setVisible(true);
?? ??? ?setLocation(400, 320);
?? ??? ?setFont(new Font("宋体", Font.PLAIN, 14));
?? ?}

?? ?public void init() {
?? ??? ?setLayout(new FlowLayout());
?? ??? ?jp = new JPanel();

?? ??? ?jl = new JLabel("序 ?号:");
?? ??? ?jl.setFont(new Font("宋体", Font.PLAIN, 14));
?? ??? ?jl.setForeground(Color.CYAN);
?? ??? ?jp.add(jl);

?? ??? ?// 实例化JTextField标签对象化
?? ??? ?text = new JTextField();
?? ??? ?Dimension dim1 = new Dimension(300, 30);
?? ??? ?text.setPreferredSize(dim1);// 设置除顶级容器组件以外其他组件的大小
?? ??? ?jp.add(text);

?? ??? ?jl1 = new JLabel(" 广告名: ");
?? ??? ?jl1.setFont(new Font("宋体", Font.PLAIN, 14));
?? ??? ?jl1.setForeground(Color.CYAN);
?? ??? ?jp.add(jl1);

?? ??? ?// 实例化
?? ??? ?text1 = new JTextField();

?? ??? ?// 设置大小
?? ??? ?text1.setPreferredSize(dim1);
?? ??? ?// 添加到窗体
?? ??? ?jp.add(text1);

?? ??? ?jl2 = new JLabel("广告内容:");
?? ??? ?jl2.setFont(new Font("宋体", Font.PLAIN, 14));
?? ??? ?jl2.setForeground(Color.CYAN);
?? ??? ?jp.add(jl2);

?? ??? ?text2 = new JTextField();

?? ??? ?// 设置大小
?? ??? ?text2.setPreferredSize(dim1);
?? ??? ?// 添加到窗体
?? ??? ?jp.add(text2);
?? ??? ?jl3 = new JLabel("审核状态:");
?? ??? ?jl3.setFont(new Font("宋体", Font.PLAIN, 14));
?? ??? ?jl3.setForeground(Color.CYAN);
?? ??? ?jp.add(jl3);

?? ??? ?text3 = new JTextField();

//设置大小
?? ??? ?text3.setPreferredSize(dim1);
//添加到窗体
?? ??? ?jp.add(text3);
?? ??? ?jl4 = new JLabel(" 时 ? 间:");
?? ??? ?jl4.setFont(new Font("宋体", Font.PLAIN, 14));
?? ??? ?jl4.setForeground(Color.CYAN);
?? ??? ?jp.add(jl4);

?? ??? ?text4 = new JTextField();

//设置大小
?? ??? ?text4.setPreferredSize(dim1);
//添加到窗体
?? ??? ?jp.add(text4);
?? ??? ?jl5 = new JLabel(" 点击率: ");
?? ??? ?jl5.setFont(new Font("宋体", Font.BOLD, 14));
?? ??? ?jl5.setForeground(Color.CYAN);
?? ??? ?jp.add(jl5);

?? ??? ?text5 = new JTextField();

?? ??? ?text5.setPreferredSize(dim1);
//添加到窗体
?? ??? ?jp.add(text5);

?? ??? ?// 实例化JButton组件
?? ??? ?b = new JButton();
?? ??? ?// 设置按键的显示内容
?? ??? ?Dimension dim2 = new Dimension(100, 30);
?? ??? ?b.setText("确定");
?? ??? ?b.setFont(new Font("宋体", Font.PLAIN, 14));
?? ??? ?// 设置按键大小
?? ??? ?b.setSize(dim2);
?? ??? ?ImageIcon icon=new ImageIcon("pic/829.jpg");//背景图
?? ? ? ? ?JLabel label = new JLabel(icon);//往一个标签中加入图片
?? ? ? ?
?? ? ? ? ?label.setBounds(0, 0, icon.getIconWidth(),icon.getIconHeight());//设置标签位置大小为图片大小
?? ? ? ? ?this.getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE));//标签添加到第二层面板
?? ? ? ? ?
?? ? ? ? ?//获取顶层容器设为透明
?? ? ? ? ?JPanel imPanel = (JPanel)getContentPane();
?? ? ? ? ?imPanel.setOpaque(false);
?? ? ? ? ?
?? ? ? ? ?//建立透明文本显示区面板 ? ?
?? ? ? ? ?JPanel displayPanel=new JPanel();
?? ? ? ? ?displayPanel.setOpaque(false);
?? ??? ?add(jp);
?? ??? ?add(jl);
?? ??? ?add(text);
?? ??? ?add(jl1);
?? ??? ?add(text1);
?? ??? ?add(jl2);
?? ??? ?add(text2);
?? ??? ?add(jl3);
?? ??? ?add(text3);
?? ??? ?add(jl4);
?? ??? ?add(text4);
?? ??? ?add(jl5);
?? ??? ?add(text5);
?? ??? ?add(b);
?? ??? ?b.addActionListener(this); // 注册事件

?? ?}

?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?MyWindow1 mywindow = new MyWindow1();

?? ?}

?? ?@Override
?? ?public void actionPerformed(ActionEvent e) {
?? ??? ?// TODO Auto-generated method stub

?? ??? ?guanggao_Dao ga1 = new guanggao_Dao();

?? ??? ?guanggao gao = new guanggao(text.getText(), text1.getText(), text2.getText(), text3.getText(), text4.getText(),
?? ??? ??? ??? ?text5.getText());
?? ??? ?ArrayList<guanggao> list = new ArrayList<guanggao>();

?? ??? ?ga1.Insert(gao);
?? ??? ?ArrayList<guanggao> g = ga1.search();
?? ??? ?list.addAll(g);

?? ?}

}
6.修改数据窗口

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Mywindow4 extends JFrame implements ActionListener {

?? ?JTextField text, text1;
?? ?JLabel jl, jl1;
?? ?JPanel jp;
?? ?JButton b;

?? ?public Mywindow4() {
?? ??? ?init();
?? ??? ?setTitle("修改数据页面");
?? ??? ?setSize(450, 400);
?? ??? ?setVisible(true);
?? ??? ?setLocation(400, 320);
?? ?}

?? ?public void init() {

?? ??? ?setLayout(new FlowLayout());
?? ??? ?jp = new JPanel();

?? ??? ?jl = new JLabel(" 请输入修改的广告名 ?:");
?? ??? ?jl.setFont(new Font("宋体", Font.PLAIN, 14));
?? ??? ?jl.setForeground(Color.green);

?? ??? ?jp.add(jl);

?? ??? ?// 实例化JTextField标签对象化
?? ??? ?text = new JTextField(20);
?? ??? ?Dimension dim1 = new Dimension(300, 150);
?? ??? ?text.setPreferredSize(dim1);// 设置除顶级容器组件以外其他组件的大小

?? ??? ?jp.add(text);
?? ??? ?jl1 = new JLabel("请输入修改的审核状态:");
?? ??? ?jl1.setFont(new Font("宋体", Font.PLAIN, 15));
?? ??? ?jl1.setForeground(Color.green);

?? ??? ?jp.add(jl1);

?? ??? ?// 实例化JTextField标签对象化
?? ??? ?text1 = new JTextField(20);
?? ??? ?Dimension dim2 = new Dimension(300, 30);
?? ??? ?text.setPreferredSize(dim2);// 设置除顶级容器组件以外其他组件的大小
?? ??? ?jp.add(text1);

?? ??? ?// 实例化JButton组件
?? ??? ?b = new JButton();
?? ??? ?// 设置按键的显示内容
?? ??? ?Dimension dim3 = new Dimension(300, 30);
?? ??? ?b.setText("确定");
?? ??? ?b.setFont(new Font("宋体", Font.PLAIN, 14));
?? ??? ?// 设置按键大小
?? ??? ?b.setSize(dim3);
?? ??? ?ImageIcon image=new ImageIcon("pic/OIP-C (1).jpg");
?? ??? ?JLabel label=new JLabel(image);
?? ??? ?jp.add(label);
?? ??? ?add(jp);
?? ??? ?add(jl);
?? ??? ?add(text);
?? ??? ?add(jl1);
?? ??? ?add(text1);
?? ??? ?add(b);

?? ??? ?b.addActionListener(this); // 注册事件
?? ?}

?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?MyWindow1 mywindow = new MyWindow1();

?? ?}

?? ?@Override
?? ?public void actionPerformed(ActionEvent e) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?guanggao_Dao ga1 = new guanggao_Dao();

?? ??? ?guanggao gao = new guanggao(text.getText(), text1.getText());

?? ??? ?ArrayList<guanggao> list = new ArrayList<guanggao>();

?? ??? ?ga1.Updata(gao);
?? ??? ?
?? ??? ?ArrayList<guanggao> g = ga1.search();
?? ??? ?list.addAll(g);

?? ?}

}
7.删除数据窗口

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.*;
import javax.swing.JTextArea;

public class Mywindow5 extends JFrame implements ActionListener {
?? ?JTextField text;
?? ?JLabel jl;
?? ?JPanel jp, jp1;
?? ?JButton b;

?? ?public Mywindow5()

?? ?{
?? ??? ?init();
?? ??? ?setTitle("删除数据页面");
?? ??? ?setSize(450, 350);
?? ??? ?setVisible(true);
?? ??? ?setLocation(400, 320);
?? ??? ?setFont(new Font("宋体", Font.PLAIN, 14));
?? ?}

?? ?public void init() {
?? ??? ?setLayout(new FlowLayout());
?? ??? ?jp = new JPanel();
?? ??? ?jp.setLayout(new FlowLayout());
?? ??? ?jp1 = new JPanel();
?? ??? ?jp1.setLayout(new BorderLayout());
?? ??? ?jl = new JLabel("请输入删除的序号:");
?? ??? ?jl.setForeground(Color.PINK);
?? ??? ?jl.setFont(new Font("宋体", Font.BOLD, 16));
?? ??? ?jp.add(jl);

?? ??? ?// 实例化JTextField标签对象化
?? ??? ?text = new JTextField(15);
?? ??? ?Dimension dim1 = new Dimension(300, 30);
?? ??? ?text.setPreferredSize(dim1);// 设置除顶级容器组件以外其他组件的大小
?? ??? ?jp.add(text);

?? ??? ?// 实例化JButton组件
?? ??? ?b = new JButton();

?? ??? ?b.setText("确定");
?? ??? ?b.setFont(new Font("宋体", Font.BOLD, 16));
?? ??? ?// 设置按键大小
?? ??? ?this.add(jp, BorderLayout.CENTER);
?? ??? ?jp1.add(b);
?? ??? ?this.add(b, BorderLayout.NORTH);
?? ??? ?ImageIcon image=new ImageIcon("pic/OIP-C.jpg");
?? ??? ?JLabel label=new JLabel(image);
?? ??? ?jp.add(label);
?? ??? ?add(jp);
?? ??? ?add(jl);
?? ??? ?add(text);

?? ??? ?add(b);
?? ??? ?b.addActionListener(this); // 注册事件
?? ?}

?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?MyWindow1 mywindow = new MyWindow1();

?? ?}

?? ?@Override
?? ?public void actionPerformed(ActionEvent e) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?guanggao_Dao ga1 = new guanggao_Dao();

?? ??? ?guanggao gao = new guanggao(text.getText());
?? ??? ?ArrayList<guanggao> list = new ArrayList<guanggao>();

?? ??? ?try {
?? ??? ??? ?ga1.Delete(gao);
?? ??? ?} catch (SQLException e1) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e1.printStackTrace();
?? ??? ?}
?? ??? ?ArrayList<guanggao> g = ga1.search();
?? ??? ?list.addAll(g);

?? ?}
}
8.查看数据窗口

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import extra.zy_Dao.*;

public class Mywindow6 extends JFrame {
?? ?private JScrollPane scpDemo;
?? ?private JTableHeader jth;
?? ?private JTable tabDemo;

?? ?public Mywindow6() {
?? ??? ?init();
?? ??? ?setTitle("运行后的数据页面");
?? ??? ?setSize(600, 500);
?? ??? ?setVisible(true);
?? ??? ?setLocation(400, 320);
?? ??? ?setFont(new Font("宋体", Font.PLAIN, 14));
?? ?}

?? ?public void init() {
?? ??? ?setLayout(new BorderLayout());
?? ??? ?setLayout(new FlowLayout());
?? ??? ?this.scpDemo = new JScrollPane();
?? ??? ?scpDemo.setSize(500, 600);
?? ??? ?ArrayList<guanggao> list = new ArrayList<guanggao>();
?? ??? ?guanggao_Dao ga = new guanggao_Dao();
?? ??? ?
?? ??? ? ga.re(null);
?? ??? ? ArrayList<guanggao> g = ga.search();
?? ??? ??? ?list.addAll(g);
?? ??? ?list.addAll(g);
?? ??? ?JTable table = new JTable();// 创建表格
?? ??? ?String[] title = { "序号", "广告名", "广告内容", "审核状态", "时间", "点击率" };// 表头信息
?? ??? ?// 创建表格模型
?? ??? ?DefaultTableModel tableModel = new DefaultTableModel(null, title);
?? ??? ?for (guanggao gao : list) {
?? ??? ??? ?Object[] rowData = { gao.getId(), gao.getName(), gao.getContent(), gao.getCheck(), gao.getTime(),
?? ??? ??? ??? ??? ?gao.getPoint() };
?? ??? ??? ?tableModel.addRow(rowData);// 把ArrayList中的数据放入表格模型
?? ??? ?}
?? ??? ?table.setModel(tableModel);
?? ??? ?// 表格间距
?? ??? ?// 美化
?? ??? ?JTableHeader header = table.getTableHeader();
?? ??? ?header.setBackground(Color.yellow);
?? ??? ?header.setFont(new Font("宋体", Font.BOLD, 16));
//窗口上下栏间距
?? ??? ?table.setSize(630, 450);

?? ??? ?// 把表格添加到窗口中
?? ??? ?add(new JScrollPane(table), BorderLayout.SOUTH);// 窗口的默认布局是边界布局

?? ?}

?? ?public static void main(String[] args) {
?? ??? ?Mywindow6 m6 = new Mywindow6();
?? ?}

}

实现截图:

文章来源:https://blog.csdn.net/NLxxxxX/article/details/135730416
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。