📄 updatedata.java
字号:
/**
*
*/
/**
* @author Linden
* @date 下午09:11:082007-10-20
*/
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class UpdateData extends JFrame implements ActionListener,
WindowListener{
Label lb1, lb2, lb3, lb4, lb5;
TextField tf1, tf2, tf3, tf4;
Button button1, button2;
Connection con;
ResultSet rs;
Statement st;
String str1 = "";
String bookname, bookkind, bookprice;
public UpdateData() {
super("更改书目");
Container c = getContentPane();
c.setLayout(null);
lb1 = new Label("书号");
lb1.setSize(120, 20);
lb1.setLocation(10, 20);
c.add(lb1);
tf1 = new TextField();//
tf1.setSize(120, 20);
tf1.setLocation(150, 20);
c.add(tf1);
button1 = new Button("查询");
button1.setSize(120, 20);
button1.setLocation(280, 20);
button1.addActionListener(this);
c.add(button1);
/*
* lb5=new Label("ebook_id");//获取上面的ID号rs.getText("ebook_id")
* lb5.setSize(120, 20); lb5.setLocation(150, 20); add(lb5);
*/
lb2 = new Label("书名");
lb2.setSize(120, 20);
lb2.setLocation(10, 40);
add(lb2);
tf2 = new TextField();// 书名
tf2.setSize(120, 20);
tf2.setLocation(150, 40);
c.add(tf2);
lb3 = new Label("类型");
lb3.setSize(120, 20);
lb3.setLocation(10, 60);
c.add(lb3);
tf3 = new TextField();// 类型
tf3.setSize(120, 20);
tf3.setLocation(150, 60);
c.add(tf3);
lb4 = new Label("价格");
lb4.setSize(120, 20);
lb4.setLocation(10, 80);
c.add(lb4);
tf4 = new TextField();// 价格
tf4.setSize(120, 20);
tf4.setLocation(150, 80);
c.add(tf4);
button2 = new Button("执行更改");
button2.setSize(120, 20);
button2.setLocation(180, 100);
button2.addActionListener(this);
c.add(button2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
setVisible(true);
connect_db();
}
public void connect_db() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e1) {
System.out.println("没能发现驱动程序!");
}
try {
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=EBook";
String user = "sa"; // sqlserver 连接
String password = "123456";
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
} catch (SQLException e2) {
System.out.println("没能发现数据库!");
}
}
public void close_db() {
try {
st.close();
con.close();
} catch (SQLException e2) {
System.out.println("发生错误,不能关闭数据库");
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
// 处理查询数据
try {
str1 = tf1.getText().toString();
rs = st.executeQuery("select * from EBook where ebook_id='"
+ str1 + "'");
while (rs.next()) {
// str1=str1+rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getFloat(4)+"\n";
bookname = rs.getString(2);
bookkind = rs.getString(3);
bookprice = rs.getString(4);
}
tf2.setText(bookname);
tf3.setText(bookkind);
tf4.setText(bookprice);
JOptionPane.showMessageDialog(null, "查询成功!");
} catch (SQLException e2) {
System.out.println("没能发现数据库!");
}
// setVisible(true);
} else if (e.getSource() == button2) {
try {
PreparedStatement pstmt;
int bookno = Integer.parseInt(tf1.getText());
String name = tf2.getText();
String kind = tf3.getText();
float price = Float.parseFloat(tf4.getText());
pstmt = con
.prepareStatement("update EBook set ebookName = ?,ebookKind = ?,ebookPrice=? where ebook_id = ?");
pstmt.setString(1, name);
pstmt.setString(2, kind);
pstmt.setFloat(3, price);
pstmt.setInt(4, bookno);
pstmt.executeUpdate();
pstmt.close();
JOptionPane.showMessageDialog(null, "数据更改成功!");
} catch (SQLException e2) {
System.out.println("没能发现数据库!");
}
}
}
/* (non-Javadoc)
* @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
*/
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
*/
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
*/
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
close_db();
System.exit(1);
}
/* (non-Javadoc)
* @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
*/
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
*/
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
*/
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
*/
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -