| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 732 人关注过本帖
标题:java与access数据库操作(jdbc-odbc桥接),我错在哪里了?
只看楼主 加入收藏
dadongzicool
Rank: 6Rank: 6
等 级:贵宾
威 望:11
帖 子:209
专家分:474
注 册:2009-3-20
结帖率:100%
收藏
 问题点数:0 回复次数:1 
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 数据库 桥接 
2009-10-22 16:06
dadongzicool
Rank: 6Rank: 6
等 级:贵宾
威 望:11
帖 子:209
专家分:474
注 册:2009-3-20
收藏
得分:0 
来个人帮我看看呗

本人出售软件专业毕业设计(包括论文,代码)题目众多,价格从优联系方式:QQ
1635008514(寻找高校代理)
2009-10-22 21:38
快速回复:java与access数据库操作(jdbc-odbc桥接),我错在哪里了?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025887 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved