swaggy郭钰轩

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

这周主要学习的是①数据库的使用以及如何去配置数据库②回顾了以前所学的概念。
JDBC
JDBC的全称是Java Database Connectivity。
  
JDBC对数据库进行操作的流程:

连接数据库
发送数据请求,即传统的CRUD指令
返回操作结果集

JDBC中常用的对象包括:

ConnectionManager
Connection
Statement
CallableStatement
PreparedStatement
ResultSet
SavePoint

如何建立数据库连接

1)加载数据库驱动,即上文中的driver以及Class.forName(dirver)
2)定位数据库连接字符串, 即dbURL以及DriverManager.getConnection(dbURL)

不同的数据库,对应的dirver和dbURL不同,但加载驱动和建立连接的方式是相同的,即只需要修改上面driver和dbURL的值就可以了。

Driver接口
1.装载MySql驱动:Class.forName("com.mysql.jdbc.Driver"); 参数为连接串,实际上是一个具体的驱动类的全名 new A() --Class.forName(“com.yan.A”).newInstance()
2.装载Oracle驱动:Class.forName("oracle.jdbc.driver.OracleDriver")
Driver可分为以下4中类型:
1.JDBC-ODBC Bridge和ODBC Driver 2.Native-API partly-Java Driver 3.JDBC-Net All-Java Driver 4.Native-protocol All-Java Driver
MySQL指令

1.显示数据库 :show databases 
显示表 :show tables
2.创建数据库testdb
create database testdb;
3.预防性创建数据库:
create database if not testdb;
4.创建表
use testdb; 
create table table1( username varchar(12), password varchar(20));
5.查看表结构
describe table1;
6.给表添加一列
alter table table1 add column(sex varchar(2) comment '性别’,age date not null comment '年龄');
commit;
7.修改表结构
create table tmp as select * from table1;
8.删除表table1
drop table if exists table1; 
drop table if exists tmp;
9.删除数据库testdb 
drop database testdb;

源代码

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;
    JMenuItem exiti;
    ImageIcon image1;
    ImageIcon image2;
    ImageIcon image3;
    ImageIcon image4;
    ImageIcon image5;
    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");
    image5 = 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);
    exiti = new JMenuItem("退出",image5);
    
   
    text.setEditable(true);
    frame.getContentPane().add(scr);
    
    newi.setMnemonic('N');
    openi.setMnemonic('O');
    closei.setMnemonic('C');
    exiti.setMnemonic('E');
    
    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));
    exiti.setAccelerator(KeyStroke.getKeyStroke('E',java.awt.Event.CTRL_MASK));
    
    newi.addActionListener(this);
    openi.addActionListener(this);
    savei.addActionListener(this);
    closei.addActionListener(this);
    exiti.addActionListener(this);
    
    
    menu.add(newi);
    menu.add(openi);
    menu.add(savei);
    menu.add(closei);
    menu.add(exiti);
    bar.add(menu);
    
    frame.addWindowListener(new MyWindowAdapter());
    
    frame.setJMenuBar(bar);
    
    frame.setSize(600, 500);
    frame.setLocation(300,200);
    frame.setVisible(true);
}
@Override
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 十四周;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.*;
import java.awt.*;
public class text1 {

    public static void main(String[] args) {
        new text();
    }

}


posted on 2019-11-29 14:11  gggyx  阅读(249)  评论(0编辑  收藏  举报

导航