简单记事本的实现:
1)实验代码:
package Rectangle;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import static javax.swing.KeyStroke.*;
public class TextPad {
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 TextPad(){
frame =new JFrame("记事本");
bar =new JMenuBar();
fileMenu =new JMenu("文件");
editMenu =new JMenu("编辑");
newIcon=new ImageIcon("d:"+File.separator+"Test"+File.separator+"A.png");
openIcon=new ImageIcon("d:"+File.separator+"Test"+File.separator+"B.png");
saveIcon=new ImageIcon("d:"+File.separator+"Test"+File.separator+"C.png");
closeIcon=new ImageIcon("d:"+File.separator+"Test"+File.separator+"D.png");
newItem=new JMenuItem("新建(N)",newIcon);
openItem=new JMenuItem("打开(O)",openIcon);
saveItem=new JMenuItem("另存为(A)",saveIcon);
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_A);
closeItem.setMnemonic(KeyEvent.VK_X);
newItem.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.CTRL_MASK));
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(closeItem);
bar.add(fileMenu);
bar.add(editMenu);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0){
System.exit(1);
}});
frame.setJMenuBar(bar);
frame.add(scroll);
frame.setVisible(true);
frame.setSize(300,180);
frame.setLocation(300,200);
}
}
JDBC
Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。JDBC也是Sun Microsystems的商标。我们通常说的JDBC是面向关系型数据库的
JDBC(Java DataBase Connectivity)是Java和数据库之间的一个桥梁,是一个规范而不是一个实现,能够执行SQL语句。它由一组用Java语言编写的类和接口组成。各种不同类型的数据库都有相应的实现.
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");
创建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"));
}
}
}