📄 selectdatabyname.java
字号:
/**
*
*/
/**
* @author Linden
* @date 下午10:06:532007-10-20
*/
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SelectDataByName extends JFrame implements ActionListener, WindowListener {
/**
* @project EBook
*/
JLabel lb1;
JTextField tf1;
JButton bu1;
JTextArea ta1;
Connection con;
ResultSet rs;
Statement st;
String str1="";
String str2="";
public SelectDataByName(){
super("根据书名查询");
addWindowListener(this);
Container c=getContentPane();
c.setLayout(null);
lb1=new JLabel("请输入书名");
lb1.setSize(120,20);
lb1.setLocation(10,20);
c.add(lb1);
tf1=new JTextField();
tf1.setSize(120,20);
tf1.setLocation(130, 20);
c.add(tf1);
bu1=new JButton("查询");
bu1.setSize(60,20);
bu1.setLocation(360, 20);
bu1.addActionListener(this);
c.add(bu1);
ta1=new JTextArea();
ta1.setSize(420,300);
ta1.setLocation(20, 50);
c.add(ta1);
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);
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){
try{
str2=tf1.getText().toString();
rs=st.executeQuery("select * from EBook where ebookName='"+str2+"'");
str1="";
while(rs.next()){
str1=str1+rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getFloat(4)+"\n";
}
ta1.setText(str1);
}catch(SQLException e2) {
System.out.println("没能发现数据库!");
}
}
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
close_db();
System.exit(1);
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -