• 第十四周课程总结&实验报告


    本周总结

    • JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。
    • MySQL的常用命令
      • 连接数据库:mysql -u用户名 -p密码
      • 创建数据库:CREATE DATABASE 数据库名称
      • 删除数据库:DROP DATABASE 数据库名称
      • 使用数据库:USE 数据库名称
      • 删除数据库表: DROP TABLE 表名称
      • 查看表结构: DESC 表名称
      • 查看全部数据库: SHOW DATABASES
      • 查看一个数据库的全部表:SHOW TABLES
    • 连接和关闭数据库:DriberMaager类的常用方法
    • MySQL数据库的连接地址格式:jdbc:mysql://IP地址:端口号/数据库名称
    • Connection接口的常用方法:创建一个对象的方法createStatement();得到数据库的元数据的方法getMetaData();关闭数据库的方法close()等等
    • Statement接口
      • Statemen接口可以通过Connection接口中提供的createStatement()方法实例化
      • 数据库更新的方法executeUpdata()
      • 执行数据库查询操作,返回一个结果集对象的方法executeQuery()
      • 增加一个待执行SQL语句的方法addBatch()
      • 关闭Statement操作的方法close()
    • ResultSet接口
      • 要进行数据库查询,需要使用Statement接口定义的executeQuery()方法
      • 将指针移到下一行的方法next()
      • 取得指定列的内容的方法next()
      • 以Date的形式按列的编号取得指定列的内容的方法getDate()

    简单记事本的实验

    实验代码

    package 简单记事本;
    
    import javax.swing .*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    public class text extends JFrame implements ActionListener{
        JFrame frame;
        JTextArea text;
        JScrollPane scr;
        JMenuBar bar;
        JMenu menu;
        JMenuItem newi;
        JMenuItem openi;
        JMenuItem savei;
        JMenuItem closei;
        ImageIcon image1;
        ImageIcon image2;
        ImageIcon image3;
        ImageIcon image4;
        JFileChooser chooser;
        File file;
        FileInputStream fil;
        FileOutputStream fol;
        
    public  text() {
        frame = new JFrame("记事本");
        text = new JTextArea();
        scr = new JScrollPane(text);
        
        image1 = new ImageIcon("D:"+File.separator+"Java"+File.separator+"picture图标"+File.separator+"新建.png");
        image2 = new ImageIcon("D:"+File.separator+"Java"+File.separator+"picture图标"+File.separator+"打开.png");
        image3 = new ImageIcon("D:"+File.separator+"Java"+File.separator+"picture图标"+File.separator+"另存为.png");
        image4 = new ImageIcon("D:"+File.separator+"Java"+File.separator+"picture图标"+File.separator+"关闭.jpg");
        
        bar = new JMenuBar();
        menu = new JMenu("文件");
        newi = new JMenuItem("新建",image1);
        openi = new JMenuItem("打开",image2);
        savei = new JMenuItem("另存为",image3);
        closei = new JMenuItem("关闭",image4);
        
       
        text.setEditable(true);
        frame.getContentPane().add(scr);
        
        newi.setMnemonic('N');
        openi.setMnemonic('O');
        closei.setMnemonic('C');
        
        newi.setAccelerator(KeyStroke.getKeyStroke('N',java.awt.Event.CTRL_MASK));
        openi.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.CTRL_MASK));
        savei.setAccelerator(KeyStroke.getKeyStroke('S',java.awt.Event.CTRL_MASK));
        closei.setAccelerator(KeyStroke.getKeyStroke('C',java.awt.Event.CTRL_MASK));
        
        newi.addActionListener(this);
        openi.addActionListener(this);
        savei.addActionListener(this);
        closei.addActionListener(this);
        
        
        menu.add(newi);
        menu.add(openi);
        menu.add(savei);
        menu.add(closei);
        bar.add(menu);
        
        frame.addWindowListener(new MyWindowAdapter());
        
        frame.setJMenuBar(bar);
        
        frame.setSize(600, 500);
        frame.setLocation(300,200);
        frame.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) {
        file =null;
        Object obj = e.getSource();
        if(obj instanceof JMenuItem) {
            JMenuItem item = (JMenuItem)obj;
            if(item == newi) {
                new text();
            }else if(item == openi) {
                chooser = new JFileChooser();
                chooser.showSaveDialog(null);
                file = chooser.getSelectedFile();
                try {
                    fil = new FileInputStream(file);
                    byte[] b = new byte[fil.available()];
                    fil.read(b);
                    String str = new String(b);
                    text.append(str);
                    fil.close();
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }else if(item == savei) {
                chooser = new JFileChooser();
                chooser.showSaveDialog(null);
                file = chooser.getSelectedFile();
                
                    try {
                        if(!file.exists()) {
                           file.createNewFile();
                        }
                        fol = new FileOutputStream(file);
                        byte[] b = text.getText().getBytes();
                        fol.write(b);
                        fol.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                
            }else if(item == closei){
                System.exit(1);
            }
        }
        
      }
    
    }
    
    package 简单记事本;
    
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    public class MyWindowAdapter  extends WindowAdapter{
        public void WindowClosing(WindowEvent arg0) {
            System.exit(1);
        }
    
    }
    
    package 简单记事本;
    
    public class demo {
        public static void main(String[] args) {
            new text();
        }
    }
    

    运行结果截图



  • 相关阅读:
    多组件共享-vuex —— 使用vuex 报错 actions should be function or object with ”handler“
    时间复杂度/空间复杂度
    Nodejs学习(三)-安装nodejs supervisor,提高点效率吧。
    Nodejs学习(二)-express生成器
    Nodejs学习(一)-Nodejs和express的安装和配置
    PHP连接MySQL的时候报错SQLSTATE[HY000] [2002] No such file or directory
    phpstorm 16.1 注册码
    Express安装过程
    NodeJs解析web一例
    NodeJs 连接mysql一例。
  • 原文地址:https://www.cnblogs.com/LuZhenYu/p/11957039.html
Copyright © 2020-2023  润新知