Learning Summary:
a, JDBC:
1.JDBC provided in a platform-independent standard database operations interfaces and classes, as long as the support of java database vendors, provided the database can use JDBC to operate.
2.JDBC primary action classes and interfaces:
3.JDBC steps:
(1) load the driver: driver has generated the respective database provider.
(2) connect to the database: To provide a connection path connection, a user name and password.
(3) Example of Operation: Statement or PreparedStatement objects instantiated by the connection object.
(4) Operation databases: Statement PreparedStatemen operation or, if the query, the query result that all ResultSet reception.
Two, MySQL database: It is a small relational database management systems.
Installation and configuration:
常用命令:
连接MySQL数据库:mysql -u用户名 -p密码
创建数据库:CREATE DATABASE 数据库名称
删除数据库:DROP DATABASE 数据库名称
使用数据库:USE 数据库名称
创建数据库表:
CREATE TABLE 表名称(
字段名称1 字段类型[DEFAULT 默认值][约束],
字段名称2 字段类型[DEFAULT 默认值][约束],
...,
字段名称n 字段类型[DEFAULT 默认值][约束]
)
删除数据库表:DROP TABLE 表名称;
查看表结构:DESC 表名称;
查看数据库信息:
查看全部数据库:SHOW ADTABASES;
查看一个数据库的全部表:SHOW TABLES;
Experimental: notepad
implementation code:
package jishi;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Notepad implements ActionListener {
JFrame frame;
JMenuBar bar;
JMenu fileMenu,editMenu;
JMenuItem newItem,openItem,saveItem,closeItem;
ImageIcon newIcon,openIcon,saveIcon,closeIcon;
JScrollPane scroll;
JTextArea area;
JFileChooser chooser;
File file;
public Notepad(){
frame = new JFrame("记事本");
bar = new JMenuBar();
fileMenu = new JMenu("文件(F)");
editMenu = new JMenu("编辑(E)");
area = new JTextArea();
scroll = new JScrollPane(area);
newItem = new JMenuItem("新建(N)",newIcon);
openItem = new JMenuItem("打开(O)",newIcon);
saveItem = new JMenuItem("保存(S)",newIcon);
saveItem = new JMenuItem("另存为(A)",newIcon);
closeItem = new JMenuItem("关闭(X)",newIcon);
JScrollPane scroll = new JScrollPane(area, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//加监听器
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
saveItem.addActionListener(this);
closeItem.addActionListener(this);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(saveItem);
fileMenu.add(closeItem);
newItem.setMnemonic(KeyEvent.VK_N);
newItem.setAccelerator(KeyStroke.getKeyStroke('N', java.awt.Event.CTRL_MASK));
openItem.setMnemonic(KeyEvent.VK_D);
openItem.setAccelerator(KeyStroke.getKeyStroke('D', java.awt.Event.CTRL_MASK));
closeItem.setMnemonic(KeyEvent.VK_K);
closeItem.setAccelerator(KeyStroke.getKeyStroke('K', java.awt.Event.CTRL_MASK));
saveItem.setMnemonic(KeyEvent.VK_S);
saveItem.setAccelerator(KeyStroke.getKeyStroke('S', java.awt.Event.CTRL_MASK));
bar.add(fileMenu);
bar.add(editMenu);
frame.setJMenuBar(bar);
frame.add(scroll);
frame.setSize(600,400);
frame.setVisible(true);
}
//事件监听处理
public void actionPerformed(ActionEvent event){
Object ob = event.getSource();
if(ob instanceof JMenuItem){
JMenuItem item = (JMenuItem) ob;
if(item == newItem){
new Notepad();
}
else if (item == openItem){
chooser = new JFileChooser();
chooser.showOpenDialog(null);
file = chooser.getSelectedFile();
try{
FileInputStream fs = new FileInputStream(file);
byte[] b = new byte[fs.available()];
fs.read(b);
area.append(new String(b));
fs.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
else if (item == saveItem){
chooser = new JFileChooser();
chooser.showSaveDialog(null);
file = chooser.getSelectedFile();
try{
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
byte[] b = area.getText().getBytes();
fos.write(b);
fos.close();
}catch(IOException e){
e.printStackTrace();
}
}
else if (item == closeItem){
System.exit(1);
}
}
}
}
package jishi;
public class Demo {
public static void main(String[] args) {
new Notepad();
}
}
operation result: