第十四周
学习总结
-
JDBD的主要操作类和接口:
-
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 test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.*;
import javax.swing.*;
public class TextPad implements ActionListener {
JFrame frame;
JMenuBar bar;
JMenu fileMenu, editMenu;
JMenuItem newItem, openItem, saveItem, saveasItem, closeItem;
ImageIcon newIcon, openIcon, saveIcon, saveasIcon, closeIcon;
JScrollPane scroll;
JTextArea area;
JFileChooser chooser;
File file;
public TextPad() {
frame=new JFrame("记事本");
bar=new JMenuBar();
fileMenu=new JMenu("文件");
editMenu=new JMenu("编辑");
newIcon=new ImageIcon();
openIcon=new ImageIcon();
saveIcon=new ImageIcon();
saveasIcon=new ImageIcon();
closeIcon=new ImageIcon();
newItem=new JMenuItem("新建(N)", newIcon);
openItem=new JMenuItem("打开(O)", openIcon);
saveItem=new JMenuItem("保存(S)", saveIcon);
saveasItem=new JMenuItem("另存为(A)", saveasIcon);
closeItem=new JMenuItem("关闭(X)", closeIcon);
area=new JTextArea();
scroll=new JScrollPane(area);
newItem.setMnemonic(KeyEvent.VK_N);
openItem.setMnemonic(KeyEvent.VK_O);
saveItem.setMnemonic(KeyEvent.VK_S);
saveasItem.setMnemonic(KeyEvent.VK_A);
closeItem.setMnemonic(KeyEvent.VK_X);
newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_DOWN_MASK));
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK));
saveasItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_DOWN_MASK));
closeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_DOWN_MASK));
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(saveasItem);
fileMenu.addSeparator();
fileMenu.add(closeItem);
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
saveasItem.addActionListener(this);
closeItem.addActionListener(this);
bar.add(fileMenu);
bar.add(editMenu);
frame.setJMenuBar(bar);
frame.add(scroll);
frame.setSize(400,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event){
Object obj=event.getSource();
if(obj instanceof JMenuItem) {
JMenuItem item=(JMenuItem) obj;
if(item == newItem) {
new TextPad();
}else if(item == openItem) {
chooser = new JFileChooser();
chooser.showOpenDialog(null);
file = chooser.getSelectedFile();
try {
FileInputStream fis= new FileInputStream(file);
byte[] b = new byte[fis.available()];
fis.read(b);
area.append(new String(b));
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else if(item == saveItem) {
if(file!=null) {
try {
PrintStream out = new PrintStream(new FileOutputStream(file));
out.print(this.area.getText());
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}else if(item == saveasItem) {
chooser.showSaveDialog(null);
file = chooser.getSelectedFile();
if(!file.exists()) {
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 test;
public class Text {
public static void main(String[] args) {
new TextPad();
}
}