(1)课程总结:
这周简单学习了下JDBC的内容:
JDBC API 允许用户访问任何形式的表格数据,尤其是存储在关系数据库中的数据。
执行流程:
(1)连接数据源,如:数据库。
(2)为数据库传递查询和更新指令。
(3)处理数据库响应并返回的结果。
JDBC 架构
(1)分为双层架构和三层架构。
JDBC 编程步骤
加载驱动程序:
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");
创建StatementPreparedStatement对象:
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"));
}
}
}
记事本:
代码:
package dome1;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
public class JKQ implements ActionListener {
JFrame frame;
JMenuBar bar;
JMenu fileMenu,editMenu;
JMenuItem newItem,openItem,saveItem,closeItem;
ImageIcon newIcon,openIcon,saveIcon,closeIcon;
JScrollPane scorll;
JTextArea area;
File file;
private JFileChooser chooser;
public JKQ(){
frame = new JFrame("小本本");
bar = new JMenuBar();
fileMenu = new JMenu("文件");
editMenu = new JMenu("编辑");
newItem = new JMenuItem("新建(N)",newIcon);
openItem = new JMenuItem("打开(O)",openIcon);
saveItem = new JMenuItem("另存为(A)",saveIcon);
closeItem = new JMenuItem("关闭(X)",closeIcon);
newIcon = new ImageIcon("d:"+File.separator+"新建.png");
openIcon = new ImageIcon("D:"+File.separator+"打开.png");
saveIcon = new ImageIcon("D:"+File.separator+"另存为.png");
closeIcon = new ImageIcon("D:"+File.separator+"关闭.png");
area = new JTextArea();
scorll = new JScrollPane(area);
newItem.setMnemonic(KeyEvent.VK_N);
openItem.setMnemonic(KeyEvent.VK_O);
saveItem.setMnemonic(KeyEvent.VK_A);
closeItem.setMnemonic(KeyEvent.VK_X);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(closeItem);
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
closeItem.addActionListener(this);
bar.add(fileMenu);
bar.add(editMenu);
frame.setJMenuBar(bar);
frame.add(scorll);
frame.setSize(666, 333);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj instanceof JMenuItem) {
JMenuItem item = (JMenuItem) obj;
if (item == newItem) {
new JKQ();
}else if (item == openItem) {
chooser = new JFileChooser();
chooser.showOpenDialog(null);
file = chooser.getSelectedFile();
}
}
}
}
package dome1;
public class TEST {
public static void main(String[] args) {
new JKQ();
}
}
记事本简单功能图例: