自己写的第一个小型简单的系统的学习多得
一。公共数据库类文件
import java.sql.*;
public final class Datebase {
private static String url = "jdbc:mysql://localhost:3306/booklend";
private static String user = "root";
private static String password = "410";
private Datebase() {
}
static {
try {
Class.forName("com.mysql.jdbc.Driver");//指定JDBC驱动程序的类型
} catch (ClassNotFoundException e) {
System.out.println("找不到jar");//找不到jar包时,打印输出
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);//返回连接数据库的Connection对象
}
public static void free(ResultSet rs, Statement st, Connection conn) {
try {//无后续工作时关闭数据库连接
if (rs != null)
rs.close();
if (st != null)
st.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
二。(1)事件的另一种实现方法,即内部方法:这样可以防止在别的类中调用该方法,以此来实现自己的一些要求。
public class Gong extends JFrame{
private JFrame jFrame;
public static void addmyMenu(final JFrame jFrame)//往一个JFrame框架里添加一个菜单栏,static方法,直接用类名.addmyMenu(框架对象)即可
{
JMenuBar menubar=new JMenuBar();
jFrame.setJMenuBar(menubar);
JMenu menu_do=new JMenu("操作");
menubar.add(menu_do);
JMenuItem mi_lend=new JMenuItem("借书");
menu_do.add(mi_lend);
mi_lend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jFrame.dispose();
Lend lend=new Lend();
lend.setVisible(true);
}
});
JMenuItem mi_return=new JMenuItem("还书");
menu_do.add(mi_return);
mi_return.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jFrame.dispose();
Return re=new Return();
re.setVisible(true);
}
});
}
(2)如果在这样的事件中要关闭当前对象的窗口,又要打开新的窗口,使用this.dispose()是不正确的,正确的方法是类名.this.dispose();如下:
button_quit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Search.this.dispose();//关闭实例化的Search这个对象的窗口,这个对象是Search类的一个实例化对象
Zhujiemian zhu=new Zhujiemian();
zhu.setVisible(true);
}
});
同理在这样的事件函数里设置对话框的显示位置,应该用类名.this.getX()+100和类名.this.getY()+100;
三。Resultset结果集是否为空的判断以及输出所有的结果,注意rel.next()是不断改变的,函数中出现一次,就表示指针后移一行。
try {
Connection con = Datebase.getConnection();
Statement sta = con.createStatement();
String sql = "select * from book where bookname like '%"
+ textfield.getText() + "%';";
ResultSet rel = sta.executeQuery(sql);//ResultSet接口储存结果集
textarea.setText("");
ResultSetMetaData rsmd = rel.getMetaData();//从结果集中获得元数据
int columnCount = rsmd.getColumnCount();//返回结果集中的列数
for (int i = 1; i <= columnCount; i++) {
textarea.append(rsmd.getColumnLabel(i) + '\t');//打印输出结果集中的列名
}
textarea.append("\n");
if (rel.next()) {//设置当前行的后一行为新的当前行,相当于指针后移一行。rel结果集为空时rel.next()返回值为false,不为空时,返回值为true
rel.previous();//设置当前行的前一行为新的当前行,相当于指针前移一行,只有这样才能打印第一行的结果
while (rel.next()) {
for (int j = 1; j <= columnCount; j++) {
textarea.append(rel.getString(j) + '\t');//打印结果集中每一行的值
}
textarea.append("\n");
}
} else {
label_dialog.setText("无此书!");
dialog.setLocation(this.getX() + 100, this.getY() + 100);
dialog.setVisible(true);
}
Datebase.free(rel, sta, con);//关闭数据库连接
} catch (Exception ep) {
ep.printStackTrace();
}
四。数据库中表的更新用alter,如删除表中的一列,增加一列,修改列的值的类型等等
alter table booklend rename as book;将booklend改名为book,其中rename as是关键字 |
而数据的更新用update,如将某一行中某一列的值加一,可以写为
update book set nownum=nownum+1 where bookid=111111 |
五。想文本区中添加滚动条
JPanel panel2 = new JPanel();
// panel2.setSize(650, 200);
this.add(panel2);
textarea = new JTextArea(8, 50);
textarea.setEditable(false);
panel2.add(textarea);
// textarea.setLineWrap(true);//设置文本区中的内容是否自动折行,即换行
scrollPane=new JScrollPane(textarea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//总是显示滚动条
panel2.add(scrollPane, BorderLayout.CENTER);//将scrollPane控件添加到panel2的中部