当前位置: 代码迷 >> Java相关 >> Java数据库连接转换Access->SQLServer
  详细解决方案

Java数据库连接转换Access->SQLServer

热度:509   发布时间:2013-09-12 08:41:30.0
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  网上  资料  最好  

----------------解决方案--------------------------------------------------------
你这加载的驱动都不是SQL的,肯定是不能成功。
----------------解决方案--------------------------------------------------------
我现在也在弄这个,代码和你的差不多。但是连上了,你确定你的桥接器JDBC-ODBC连接上了吗?
----------------解决方案--------------------------------------------------------
  相关解决方案