当前位置: 代码迷 >> Java相关 >> java与access数据库操作(jdbc-odbc桥接),我错在哪里了?
  详细解决方案

java与access数据库操作(jdbc-odbc桥接),我错在哪里了?

热度:400   发布时间:2009-10-22 16:06:17.0
java与access数据库操作(jdbc-odbc桥接),我错在哪里了?
数据库表的建立如图:


代码如下:

**************************************************************************

public class StudentManagement{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch(ClassNotFoundException e)
        {
            System.out.println(e);
        }
        new DatabaseWin();
    }

}
**************************************************************************
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


public class DatabaseWin extends JFrame implements ActionListener
{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    JMenuBar menubar;
    JMenu menu;
    JMenuItem itemShow,itemUpdate,itemInsert;
    ShowRecord showRecord;
    ModifyRecord modifyRecord;
    InsertRecord insertRecord;
    DatabaseWin()
    {
        menubar=new JMenuBar();
        menu=new JMenu("操作数据库");
        itemShow=new JMenuItem("显示记录");
        itemUpdate=new JMenuItem("更新记录");
        itemInsert=new JMenuItem("插入记录");
        itemShow.addActionListener(this);
        itemUpdate.addActionListener(this);
        itemInsert.addActionListener(this);
        menu.add(itemShow);
        menu.add(itemUpdate);
        menu.add(itemInsert);
        menubar.add(menu);
        showRecord=new ShowRecord("显示记录对话框");
        modifyRecord=new ModifyRecord("修改记录对话框");
        insertRecord=new InsertRecord("插入记录对话框");
        setJMenuBar(menubar);
        setBounds(100,100,370,250);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e)
    {
        // TODO Auto-generated method stub
        if(e.getSource()==itemShow)
            showRecord.setVisible(true);
        if(e.getSource()==itemUpdate);
            modifyRecord.setVisible(true);
        if(e.getSource()==itemInsert)
            insertRecord.setVisible(true);
        
    }

}
**************************************************************************
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;


public class ShowRecord extends JDialog implements ActionListener
{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    JTable table;
    Object a[][];
    Object name[]={"学号","姓名","出生日期","身高"};
    JButton showRecord;
    Connection con;
    Statement sql;
    ResultSet rs;
    public ShowRecord(String title)
    {
        // TODO Auto-generated constructor stub
        setTitle(title);
        showRecord=new JButton("显示记录");
        showRecord.addActionListener(this);
        add(showRecord,BorderLayout.NORTH);
        setBounds(200,60,400,250);
    }

    public void actionPerformed(ActionEvent e)
    {
        // TODO Auto-generated method stub
        try
        {
            con=DriverManager.getConnection("jdbc:odbc:hello","","");
            sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            rs=sql.executeQuery("SELECT*FROM message");
            rs.last();
            int lastNumber=rs.getRow();
            a=new Object[lastNumber][4];
            int k=0;
            rs.beforeFirst();
            while(rs.next())
            {
                a[k][0]=rs.getString(1);
                a[k][1]=rs.getString(2);
                a[k][2]=rs.getDate(3);
                a[k][3]=rs.getDouble(4);
                k++;
            }
            con.close();
        }
        catch(SQLException ee)
        {
            System.out.println(ee);
        }
        table=new JTable(a,name);
        getContentPane().removeAll();
        add(showRecord,BorderLayout.NORTH);
        add(new JScrollPane(table),BorderLayout.CENTER);
        validate();
    }

}
**************************************************************************
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;


public class ModifyRecord extends JDialog implements ActionListener
{

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    JLabel hintLabel;
    JTextField inputNumber;
    Object name[]={"姓名","出生日期","身高"};
    Object a[][]=new Object[1][3];
    JTable table;
    JButton enterModify;
    Connection con;
    Statement sql;
    ResultSet rs;
    String num;
    public ModifyRecord(String s)
    {
        // TODO Auto-generated constructor stub
        setTitle(s);
        hintLabel=new JLabel("输入学号(回车确认):");
        inputNumber=new JTextField(20);
        table=new JTable(a,name);
        enterModify=new JButton("更新记录");
        setLayout(null);
        Box baseBox=Box.createHorizontalBox();
        baseBox.add(hintLabel);
        baseBox.add(inputNumber);
        baseBox.add(new JScrollPane(table));
        baseBox.add(enterModify);
        add(baseBox);
        baseBox.setBounds(20,60,700,200);
    }

    public void actionPerformed(ActionEvent e)
    {
        // TODO Auto-generated method stub
        if(e.getSource()==inputNumber)
        {
            try
            {
                num=inputNumber.getText().trim();
                con=DriverManager.getConnection("jdbc:odbc:hello","","");
                sql=con.createStatement();
                rs=sql.executeQuery("SELECT*FROM message WHERE number='"+num+"'");
                boolean boo=rs.next();
                if(boo==false)
                {
                    JOptionPane.showMessageDialog(this,"学号不存在","提示",JOptionPane.WARNING_MESSAGE);
                    
                }
                else
                {
                    a[0][0]=rs.getString(2);
                    a[0][1]=rs.getDate(3).toString();
                    a[0][2]=rs.getString(4);
                    table.repaint();
                }
                con.close();
            }
        catch(SQLException ee)
        {
            System.out.println(ee);
        }
        }
        if(e.getSource()==enterModify)
        {
            try
            {
                con=DriverManager.getConnection("jdbc:odbc:hello","","");
                sql=con.createStatement();
                sql.executeUpdate("UPDATE message SET name='"+a[0][0]+"',birthday='"+a[0][1]+"',height='"+a[0][2]+"'WHERE number='"+num+"'");
                JOptionPane.showMessageDialog(this, "更新成功","成功",JOptionPane.PLAIN_MESSAGE);
               
            }
            catch(SQLException ee)
            {
                JOptionPane.showMessageDialog(this,"更新失败"+ee,"失败",JOptionPane.ERROR_MESSAGE);
            }
        }
    }

}
**************************************************************************
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.JDialog;


public class InsertRecord extends JDialog implements ActionListener
{

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    JLabel hintLabel;
    Object name[]={"学号","姓名","出生日期","身高"};
    Object a[][]=new Object[1][4];
    JTable table;
    JButton enterInsert;
    Connection con;
    Statement sql;
    ResultSet rs;
    String num;
    public InsertRecord(String s)
    {
        // TODO Auto-generated constructor stub
        setTitle(s);
        hintLabel=new JLabel("输入新纪录:");
        table=new JTable(a,name);
        enterInsert=new JButton("插入新纪录");
        setLayout(null);
        Box baseBox=Box.createHorizontalBox();
        baseBox.add(hintLabel);
        baseBox.add(new JScrollPane(table));
        baseBox.add(enterInsert);
        add(baseBox);
        baseBox.setBounds(10,40,600,38);
        enterInsert.addActionListener(this);
        setBounds(120,160,700,200);
    }

    public void actionPerformed(ActionEvent e)
    {
        // TODO Auto-generated method stub
        try
        {
            con=DriverManager.getConnection("jdbc:odbc:hello","","");
            sql=con.createStatement();
            int k=sql.executeUpdate("INSERT INTO message VALUES('"+a[0][0]+"','"+a[0][1]+"','"+a[0][2]+"','"+a[0][3]+"')");
            if(k==1)
            {
                JOptionPane.showMessageDialog(this,"插入记录成功","成功",JOptionPane.PLAIN_MESSAGE);
               
            }
            con.close();
        }
        catch(SQLException ee)
        {
            JOptionPane.showMessageDialog(this,"插入记录失败"+ee,"失败",JOptionPane.ERROR_MESSAGE);
        }
    }

}
*********************************************************************

插入数据能成功

显示数据出错

修改数据不成功

另外每一个菜单项点击后会出来两个对话框
搜索更多相关的解决方案: access  java  数据库  桥接  

----------------解决方案--------------------------------------------------------
来个人帮我看看呗
----------------解决方案--------------------------------------------------------
  相关解决方案