日期:2014-05-20  浏览次数:21173 次

如何在别的电脑上运行有数据库功能的Java程序
如题,我的程序连接的是网络MySQL数据库,数据库不在本地,摘我的电脑上运行该代码一切正常,但是换到别的电脑上就无法正常连接数据库,请问如何在别的电脑上配置MySQL驱动来达到连接数据库的目的呢?
Java代码:
Java code
import java.awt.Font;
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.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class search extends JFrame implements ActionListener{

    JLabel log=new JLabel("二级查询");
    JTextField log_t=new JTextField();
    JLabel id=new JLabel("身份证号:");
    JTextField id_t=new JTextField();
    JLabel name=new JLabel("姓名:");
    JTextField name_t=new JTextField();
    JLabel result=new JLabel("结果:");
    JTextField result_t=new JTextField();
    JLabel bishi=new JLabel("笔试:");
    JTextField bishi_t=new JTextField();
    JLabel jishi=new JLabel("机试:");
    JTextField jishi_t=new JTextField();
    JButton bt1=new JButton("查询");
    JPanel pe=new JPanel();
    
    search()
    {
        super("二级考试查询系统");
        setSize(350,230);
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);
        add(pe);
        pe.setLayout(null);
        pe.add(log);
        log.setBounds(120, 10, 100, 34);
        log.setFont(new Font("行楷",Font.BOLD,20));
        pe.add(id);
        id.setBounds(10, 50, 80, 24);
        pe.add(id_t);
        id_t.setBounds(80, 50, 180, 24);
        pe.add(bt1);
        bt1.setBounds(270, 49,70, 24);
        bt1.setFocusPainted(false);
        pe.add(name);
        pe.add(name_t);
        name.setBounds(10, 105, 40, 24);
        name_t.setBounds(55, 105, 90, 24);
        name_t.setEditable(false);
        pe.add(result);
        pe.add(result_t);
        result.setBounds(160, 105, 40, 24);
        result_t.setBounds(200, 105,90, 24);
        result_t.setEditable(false);
        pe.add(bishi);
        pe.add(bishi_t);
        bishi.setBounds(10, 160, 40, 24);
        bishi_t.setBounds(55, 160, 90, 24);
        bishi_t.setEditable(false);
        pe.add(jishi);
        pe.add(jishi_t);
        jishi.setBounds(160, 160, 40, 24);
        jishi_t.setBounds(200, 160, 90, 24);
        jishi_t.setEditable(false);
        
        bt1.addActionListener(this);
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        search smp=new search();
        smp.setVisible(true);
        smp.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){System.exit(0);}});
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        
        if(e.getSource()==bt1)
        {
            String name,result,bishi,jishi;
            try
            {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            }
            catch(ClassNotFoundException e1){
                e1.printStackTrace();
            }
            try
            {
                Connection con = DriverManager.getConnection("jdbc:mysql://208.81.166.169:3306/hythzxsql","hythzxSQL","727799796");
                //Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/二级查询","root","hythzx");
                Statement stmt = con.createStatement();
                String sql = "SELECT name,result,bishi,jishi  FROM result where id='"+id_t.getText()+"'";
                ResultSet rs = stmt.executeQuery(sql);
                while (rs.next())
                {
                
                name=rs.getString("name");
                result=rs.getString("result");
                bishi=rs.getString("bishi");
                jishi=rs.getString("jishi");
                name_t.setText(name);
                result_t.setText(result);
                bishi_t.setText(bishi);
                jishi_t.setText(jishi);
                }
                
                rs.close(); 
                stmt.close(); 
                con.close();     
            }
            catch(Exception e1){
                System.out.println("连接失败!");
                e1.printStackTrace();
            }
        }
    }

}



------解决方案--------------------
导入相应的Driver.jar就行了。驱动jar
------解决方案--------------------
你在别的电脑上是如何运行的?
你是打个jar包过去运行的?

那你打jar包的时候没有将驱动打进去。建议你安装eclipse的Fat Jar插件,如何用这个来打包
就不需要你管了


--signature-------------------
http://www.mowker.com/view/
------解决方案--------------------
1,确保驱动没有问题
mysql驱动应该放在访问端,而不是数据库端。
2,确认你们的网络是通的!
3,确保用户权限有没有问题
hythzxSQL
你用root用户试试(确认root有远程访问的权限)