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

第十四周课程总结

一、JDBC
      1. JDBC概述
      2. JDBC操作步骤
二、MySQL数据库
      1. MySQL常用命令
      2. MySQL语法基础

 

一、JDBC     

1. JDBC概述   

   JDBC提供了一种与平台无关的用于执行SQL语句的标准JavaAPI,可以方便的实现多种关系型数据库的统一操作,它由一组用Java语言编写的类和接口组成

  

 

   

JDBC的主要操作类及接口

类及接口

描述

java,sql.DriverManager

用于管理JDBC驱动程序

java,sql.Connection

用于建立与特定数据库的连接,一个连接就是一个会话,建立连接后便可以执行SQL语句和获得检索结果

java,sql.Statement

一个Statement对象用于执行静态SQL语句,并获得语句执行后产生的后果

java,sql.PreparedStatement

创建一个可以编译的SQL语句对象,该对象可以被多次运行,以提高执行的效率,该接口是Statement的子接口

java,sql.ResultSet

用于创建表示SQL语句检索结果的结果集,用户通过结果集完成对数据库的访问

java,sql.Date

该类是标准jaa,util.Date类的一个子集,用于表示SQLDATE相同的日期类型,该日期不包括时间

java,sql.TimeStamp

标准jaa,util.Date类的扩展,用于表示SQL时间戳,并增加了一个能表示ns(纳秒)的时间域

java,sql.CallableStatement

用于执行SQL存储过程

java,sql.DatabaseMetaData

与java.sql.ResultSetMetaData一同用于访问数据库的元信息

java,sql.Driver

定义一个数据库驱动程序的接口

java,sql.DataTruncation

在JDBC遇到数据截断的异常时,报告一个警告(读数据时)或产生一个异常(写数据时)

java,sql.DriverPropertyInfo

高级程序设计人员通过DriverPropertuInfo与Driver进行交流,可使用getDriverPropertyInfo获取或提供驱动程序的信息

java,sql.Time

该类是标准java.util.Date的一个子集,用于表示时分秒

java,sql.SQLException

对数据库访问时缠身的错误的描述信息

java,sql.SQLWarning

对数据库访问时产生的警告的描述信息

java,sql.Types

定义了表示SQL类型的常量

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2. JDBC操作步骤

(1)加载驱动

(2)连接数据库

(3)通过Statement发送sql语句

         通过Prepared Statement发送sql语句 

(4)关闭数据库

 

1.加载驱动程序
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";  //MySQL中的数据库驱动程序路径
Class.forName(DBDRIVER);   //利用class类加载驱动程序,需要处理异常
2.连接数据库
........接上
Connection conn = null;
conn = DriverManager.getConnetion(连接地址,用户名,密码);//需处理异常
3.执行数据库
需要使用Statement 接口完成


String sql = "INSERT INTO user(name,password,age,sex,birthday)"+"VALUES('lxh','www.ddf','30','male','2000-02')";
.......接上
Statement stmt = conn.creatStatement();
stmt.executeUpdate(sql); //执行数据库插入操作

 

二、MySQL数据库     

1. MySQL常用命令

 

连接MySQL数据库
mysql -u 用户名 -p 密码

创建数据库
创建:CREATE DATABASE 数据库名称
删除:DROP DATABASE 数据库名称

 

查看表结构
DESC 表名称

查看数据库的全部表
查看全部数据库:SHOW DATEBASE;
查看一个数据库的全部表:SHOW TABLES;

 

 

 2. MySQL语法基础


 

 

 

实验报告

一、实验源码

 

package jishi;

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 notepad 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 notepad() {
        
        
        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 notepad();
            }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 jishi;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyWindowAdapter extends WindowAdapter{
    public void WindowClosing(WindowEvent arg0) {
        System.exit(1);
    }

}          //可直接使用匿名内部类完成监听操作
package jishi;

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 notepad1 {

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

    }

}

 

 

二、实验结果

新建

打开

另存为

 

总结

知识点多而杂,需要整体把知识系统化,能更好的理解消化巩固所学。

posted @ 2019-11-28 10:33  林衍  阅读(252)  评论(1编辑  收藏  举报