第十四周课程总结&实验报告
简易记事本
(一)实验代码:
package A;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import javax.swing.*;
public class TextPad implements ActionListener {
JFrame frame;
JMenuBar bar;
JMenu fileMenu, editMenu;
ImageIcon newIcon, openIcon, saveIcon, closeIcon;
JMenuItem newItem, openItem, saveItem, closeItem;
JTextArea area;
JScrollPane scroll;
File file;
public TextPad() {
frame = new JFrame("HNC");
bar = new JMenuBar();
fileMenu = new JMenu("文件");
area = new JTextArea();
scroll = new JScrollPane(area);
newIcon = new ImageIcon("d:" + File.separator + "tupian" + File.separator + "1.jpg");
openIcon = new ImageIcon("d:" + File.separator + "tupian" + File.separator + "2.jpg");
saveIcon = new ImageIcon("d:" + File.separator + "tupian"+ File.separator + "3.jpg");
closeIcon = new ImageIcon("d:" + File.separator + "tupian" + File.separator + "4.jpg");
newItem = new JMenuItem("新建(N)", newIcon);
openItem = new JMenuItem("打开(O)", openIcon);
saveItem = new JMenuItem("保存(S)", saveIcon);
closeItem = new JMenuItem("关闭(C)", closeIcon);
newItem.setMnemonic(KeyEvent.VK_N);
openItem.setMnemonic(KeyEvent.VK_O);
saveItem.setMnemonic(KeyEvent.VK_S);
closeItem.setMnemonic(KeyEvent.VK_C);
newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
InputEvent.CTRL_MASK));
openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK));
saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_MASK));
closeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_MASK));
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
closeItem.addActionListener(this);
bar.add(fileMenu);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(closeItem);
frame.add(scroll);
frame.setJMenuBar(bar);
frame.setSize(400, 300);
frame.setLocation(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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) {
JFileChooser 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) {
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(null);
file = chooser.getSelectedFile();
if (!file.exists()) {
try {
PrintStream out = new PrintStream(new FileOutputStream(file));
out.print(area.getText());
out.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
} else if (item == closeItem) {
System.exit(0);
}
}
}
}
实验结果:
JDBC的连接地址实际上是由以下的3个部分组成。
(1)jabc 协议:JDBC URL 中的协议总是 jdbc。
(2)子协议:驱动程序名或数据库连接机制(这种机制可由一个或多个驱动程序支持)的名称,如
MySQL。
(3)子名称: 种标识数据库的方法。必须遵循“//主机名:端口/子协议”的标准URL 命名约定,
如//localhost:3306/mldn
加载驱动程序:
Class.forName(driverClass)
//加载MySql驱动
Class.forName("com.mysql.jdbc.Driver")
//加载Oracle驱动
Class.forName("oracle.jdbc.driver.OracleDriver")
获得数据库连接:
DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/imooc", "root", "root");
创建Statement\PreparedStatement对象:
conn.createStatement();
conn.prepareStatement(sql);
完整实例:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DbUtil {
public static final String URL = "jdbc:mysql://localhost:3306/imooc";
public static final String USER = "liulx";
public static final String PASSWORD = "123456";
public static void main(String[] args) throws Exception {
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2. 获得数据库连接
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
//3.操作数据库,实现增删改查
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT user_name, age FROM imooc_goddess");
//如果有数据,rs.next()返回true
while(rs.next()){
System.out.println(rs.getString("user_name")+" 年龄:"+rs.getInt("age"));
}
}
}