| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 500 人关注过本帖
标题:Java数据库连接转换Access->SQLServer
只看楼主 加入收藏
chenloveyou
Rank: 1
来 自:西安
等 级:新手上路
帖 子:16
专家分:0
注 册:2012-12-17
结帖率:20%
收藏
 问题点数:0 回复次数:2 
Java数据库连接转换Access->SQLServer
本人新手,书上好像都是Access,现在想把Access转化为SQLServer,在网上找到一些资料,但是没有成功。希望各位大神能够帮助一下,最好能把关于SQLServer链接详细说一下,谢谢了!!!


程序代码:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;


public class fwin extends JFrame implements ActionListener {
    JTable table;
    JButton b1,b2,b3,b4;
    Object a[][];
    Connection con;
    Statement sql;
    ResultSet rs;
    Object columnName[] = {"学号","姓名","数学","英语"};
    
    fwin(){
        super("数据库查询");
        a = new Object[60][4];
        table = new JTable(a,columnName);
        setSize(500,500);
        b1 = new JButton("浏览表");
        b2 = new JButton("排序");
        b3 = new JButton("查询");
        b4 = new JButton("退出");
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        Container con = getContentPane();
        con.setLayout(new FlowLayout());
        con.add(b1);
        con.add(b2);
        con.add(b3);
        con.add(b4);
        Container container = getContentPane();
        container.add(new JScrollPane(table),BorderLayout.CENTER);
        setVisible(true);
        validate();
        removeWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
            
        });
    }

    int i=0;
    public void actionPerformed1(ActionEvent ev)
    {
        if(ev.getSource() == b1)
        {
            while(i>=0)
            {
                a[i][0]="";
                a[i][1]="";
                a[i][2]="";
                a[i][3]="";
                i--;
            }
            i=0;
            try{
                Class.forName("sun.jdbc.odbc.JdbcOdacDriver");
                }
            catch(ClassNotFoundException e) {}
            try{
                con = DriverManager.getConnection("jdbc:odbc:abc","","");
                sql = con.createStatement();
                rs = sql.executeQuery("Selsec*From biao");
                while(rs.next()){
                    String number = rs.getString(i);
                    String name = rs.getString(2);
                    int math = rs.getInt("数学");
                    int eng = rs.getInt("英语");
                    a[i][0] = number;
                    a[i][1] = name;
                    a[i][2] = String.valueOf(math);
                    a[i][3] = String.valueOf(eng);
                    i++;
                }
                con.close();
                repaint();
            }catch(SQLException el){};
        }
    else if(ev.getSource() == b2)
    {
        while(i>=0)
        {
            a[i][0]="";
            a[i][1]="";
            a[i][2]="";
            a[i][3]="";
            i--;
        }
        i=0;
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdacDriver");
            }
        catch(ClassNotFoundException e) {}
        try{
            con = DriverManager.getConnection("jdbc:odbc:abc","","");
            sql = con.createStatement();
            rs = sql.executeQuery("Selsec*From biao");
            while(rs.next()){
                String number = rs.getString(i);
                String name = rs.getString(2);
                int math = rs.getInt("数学");
                int eng = rs.getInt("英语");
                a[i][0] = number;
                a[i][1] = name;
                a[i][2] = String.valueOf(math);
                a[i][3] = String.valueOf(eng);
                i++;
            }
            con.close();
            repaint();
        }catch(SQLException el){};
    }
    else if(ev.getSource()==b3)
    {
        while(i>=0)
        {
            a[i][0]="";
            a[i][1]="";
            a[i][2]="";
            a[i][3]="";
            i--;
        }
        i=0;
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdacDriver");
            }
        catch(ClassNotFoundException e) {}
        try{
            con = DriverManager.getConnection("jdbc:odbc:abc","","");
            sql = con.createStatement();
            rs = sql.executeQuery("Selsec*From biao");
            while(rs.next()){
                String number = rs.getString(i);
                String name = rs.getString(2);
                int math = rs.getInt("数学");
                int eng = rs.getInt("英语");
                a[i][0] = number;
                a[i][1] = name;
                a[i][2] = String.valueOf(math);
                a[i][3] = String.valueOf(eng);
                i++;
            }
            con.close();
            repaint();
        }catch(SQLException el){};
    }    
    else if(ev.getSource() == b4)
        dispose();
    }

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            
        }
}
    
class query{
    public static void main(String[] args)
    {
        new fwin();
    }
}
搜索更多相关主题的帖子: 数据库连接 Java 网上 资料 最好 
2013-09-12 08:41
ren829
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:15
帖 子:255
专家分:1174
注 册:2006-3-11
收藏
得分:0 
你这加载的驱动都不是SQL的,肯定是不能成功。
2013-09-12 10:55
见勤
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-9-14
收藏
得分:0 
我现在也在弄这个,代码和你的差不多。但是连上了,你确定你的桥接器JDBC-ODBC连接上了吗?
2013-09-14 08:34
快速回复:Java数据库连接转换Access->SQLServer
数据加载中...
 
   



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

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