第十四周总结

小结:这周主要学习的是数据库的使用以及如何去配置数据库,有了数据库我们可以更加好的去完善我们的程序。

一、关于JDBC

JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序,同时,JDBC也是个商标名。

二、.Driver接口

Driver接口由数据库厂家提供,作为java开发人员,只需要使用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

三、JDBC操作

1.加载驱动程序

public static final String DBDRIVER = "org.gjt.mm.mysql.Driver"; 
Class.forName(DBDRIVER);  

2.连接数据库

Connection conn = null;
conn = DriverManager.getConnetion();

四、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 test;

import javax.swing .*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
    public class un extends JFrame implements ActionListener{

        JFrame frame;
        JTextArea text;
        JScrollPane scr;
        JMenuBar bar;
        JMenu menu;
        JMenuItem newi;
        JMenuItem openi;
        JMenuItem savei;
        JMenuItem closei;
        JMenuItem exiti;
      
        JFileChooser chooser;
        File file;
        FileInputStream fil;
        FileOutputStream fol;
        
        public un() {
            
            frame = new JFrame("记事本");
            text = new JTextArea();
            scr = new JScrollPane(text);
            
            bar = new JMenuBar();
            menu = new JMenu("文件");
            newi = new JMenuItem("新建");
            openi = new JMenuItem("打开");
            closei = new JMenuItem("关闭");
            exiti = new JMenuItem("退出");
            savei = new JMenuItem("另存为");
        
            
           
            text.setEditable(true);
            frame.getContentPane().add(scr);
            
            
            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 My());
            
            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 un();
                }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);
                }
            }
            
        }
        
    }
posted @ 2019-11-28 11:52  LUM哟  阅读(177)  评论(1编辑  收藏  举报