import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
public class c5_03_05 extends JFrame implements ActionListener,WindowListener
{
public static void main(String args[])
{
c5_03_05 frame=new c5_03_05();
}
JLabel lb1;
JTextField tf1;
JButton bu1;
JTextArea ta1;
Connection con1;
ResultSet rs1;
Statement st1;
String str1="";
String str2="";
public c5_03_05()
{
super("c5_03_05");
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,280);
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("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e1)
{
System.out.println("Driver not found!");
}
try
{
con1=DriverManager.getConnection("jdbc:odbc:aa");
st1=con1.createStatement();
}catch(SQLException e2)
{System.out.println("table not found!");
}
}
public void close_db()
{
try
{
st1.close();
con1.close();
}catch(SQLException e2)
{System.out.println("error!");
}
}
public void actionPerformed(ActionEvent e)
{
String str1;
String str2;
String std_id;
try
{
str2=tf1.getText();
rs1=st1.executeQuery("select * from 简历 where 姓名='"+str2+"'");
str1="";
while(rs1.next())
{
str1=str1+rs1.getString(1)+"\t"+rs1.getString(2)+"\n";
}
ta1.setText(str1);
}catch(SQLException e2)
{
System.out.println("error!");
}
}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowClosing(WindowEvent e){
close_db();
System.exit(1);
}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
一个小例子你看一下就明白了.建一个access数据库名为:简历.mdb
用JBuilder 编辑就可以运行了.
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
public class c5_03_05 extends JFrame implements ActionListener,WindowListener
{
public static void main(String args[])
{
c5_03_05 frame=new c5_03_05();
}
JLabel lb1;
JTextField tf1;
JButton bu1;
JTextArea ta1;
Connection con1;
ResultSet rs1;
Statement st1;
String str1="";
String str2="";
public c5_03_05()
{
super("c5_03_05");
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,280);
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("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e1)
{
System.out.println("Driver not found!");
}
try
{
con1=DriverManager.getConnection("jdbc:odbc:aa");
st1=con1.createStatement();
}catch(SQLException e2)
{System.out.println("table not found!");
}
}
public void close_db()
{
try
{
st1.close();
con1.close();
}catch(SQLException e2)
{System.out.println("error!");
}
}
public void actionPerformed(ActionEvent e)
{
String str1;
String str2;
String std_id;
try
{
str2=tf1.getText();
rs1=st1.executeQuery("select * from 简历 where 姓名='"+str2+"'");
str1="";
while(rs1.next())
{
str1=str1+rs1.getString(1)+"\t"+rs1.getString(2)+"\n";
}
ta1.setText(str1);
}catch(SQLException e2)
{
System.out.println("error!");
}
}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowClosing(WindowEvent e){
close_db();
System.exit(1);
}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
一个小例子你看一下就明白了.建一个access数据库名为:简历.mdb
用JBuilder 编辑就可以运行了.
解决方案 »
- Hibernate错误求解
- jboss4中一个名为warA的war应用开启后阻塞了名为warB的war应用
- 帮我解释一下这段存储过程是啥意思
- 用java备份和还原mysql数据库
- 散分求助:关于分布式事务的2pc协议以及分布式开发的问题,解决立给分
- [转载&改编]一步一步开发Spring Framework MVC应用程序-2(Intellij IDEA版)
- JBOSS出错,急!!!
- 用James邮件服务器发邮件,总出问题。源代码附上,请大家帮着看看!
- xfire 与 springmvc 兼容问题
- spring security3 cas4整合 登陆成功后应用端获取不到用户 求大神帮忙
- 请问:在spring MVC 从没有form的jsp页浏览后台的数据, 如果需要带参数(应该到server层吧?),如何做啊
- Hibernate 3.0 即将发布
import java.io.*;
import java.lang.*;public class sqlResult{
private Connection con = null;
private Statement state = null;
private ResultSet rs = null;
private PreparedStatement pre = null;
public Connection getConnection(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbdDriver");
con = DriverManager.getConnection("jdbc:odbc:数据源名称","","");
}
catch(ClassNotFoundException e){
System.out.println("注册失败");
}
catch(SQLException e){
System.out.println("连接失败");
}
return con;
}
public ResultSet getResultSet(Connection con){
try{
state = con.createStatement(); //普通查询
}
catch(SQLException e){
System.out.println("连接失败");
}
try{
String sql = "select * from student";
rs = state.executeQuery(sql1);
}
catch(SQLException e){
System.out.println("结果失败");
}
return rs;
}
public void printResult(ResultSet rs){
try{
while(rs.next()){
System.out.println(rs.getString("姓名")+" "+rs.getString("学号")+" "+rs.getString("性别")+" "+rs.getString("年龄"));
}
}
catch(SQLException e){
System.out.println("打印显示失败");
e.printStackTrace();
}
}
public void cloze(){
try{
if(rs != null)
rs.close();
if(state != null)
state.close();
if(con != null)
con.close();
}
catch(SQLException e){
System.out.println("关闭出错");
}
}
public static void main(String[] args){
sqlResult result = new sqlResult();
Connection con = result.getConnection();
ResultSet rs = result.getResultSet(con);
result.printResult(rs);
result.cloze();
} }
//我没试过