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 编辑就可以运行了.

解决方案 »

  1.   

    可否直接连结,如:使用String strurl=“jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=文件名.mdb”
      

  2.   

    将access写成的数据库进行配置,成为odbc数据源,再用java提供的jdbc-odbc桥进行操作.
      

  3.   

    我前几天测试的import java.sql.*;
    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();

    } }
      

  4.   

    (转贴)无需配odbc数据源,上面改为String ConnStr="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};dbq=e:/jtest.mdb";这种语句稍微修改一下就可以连接支持odbc的任意数据库,而无需到控制面板去配置odbc
    //我没试过