📄 insertdata.java
字号:
/**
*
*/
/**
* @author Linden
* @date 下午07:52:242007-10-20
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class InsertData extends JFrame implements ActionListener, WindowListener {
Label b;
Label b1[] = new Label[4];
TextField tf1[] = new TextField[4];
String fieldname[] = { "书号", "书名", "类别", "价格" };
String fieldvalue[] = { "", "", "", "" };
Button bu;
TextArea ta1;
Connection con;
ResultSet rs;
PreparedStatement st;
String str1 = "";
public InsertData() {
// TODO Auto-generated constructor stub
super("插入书目");
addWindowListener(this);
Container c = getContentPane();
c.setLayout(null);
b = new Label("添加数据操作");
b.setSize(200, 20);
b.setLocation(10, 20);
c.add(b);
bu = new Button("添加记录");
bu.setSize(100, 20);
bu.setLocation(260, 20);
bu.addActionListener(this);
c.add(bu);
for (int i = 0; i < 4; i++) {
b1[i] = new Label(fieldname[i]);
b1[i].setSize(80, 20);
b1[i].setLocation(10, 50 + i * 30);
c.add(b1[i]);
tf1[i] = new TextField(fieldvalue[i]);
tf1[i].setSize(90, 20);
tf1[i].setLocation(120, 50 + i * 30);
c.add(tf1[i]);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 400);
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);
} catch (SQLException e2) {
System.out.println("没能发现数据库!");
}
}
public void close_db() {
try {
st.close();
con.close();
} catch (SQLException e2) {
System.out.println("发生错误,不能关闭数据库");
}
}
/**
* @param args
*/
public void actionPerformed(ActionEvent e) {
try {
// TODO Auto-generated method stub
int book_no = Integer.parseInt(tf1[0].getText());
String book_name = tf1[1].getText();
String book_kind = tf1[2].getText();
float book_price = Float.parseFloat(tf1[3].getText());
str1 = "insert into EBook values(?,?,?,?)";
st = con.prepareStatement(str1, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
st.setInt(1, book_no);
st.setString(2, book_name);
st.setString(3, book_kind);
st.setFloat(4, book_price);
st.executeUpdate();
JOptionPane.showMessageDialog(null, "成功添加书目!");
} catch (SQLException e2) {
System.out.println("不能插入数据到数据库!");
System.out.println(e2.getMessage());
}
}
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
close_db();
System.exit(1);
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -