| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 644 人关注过本帖
标题:在ecplise中连接Mysql的问题
只看楼主 加入收藏
yu1543054075
Rank: 1
等 级:新手上路
帖 子:102
专家分:8
注 册:2015-4-30
结帖率:90.24%
收藏
已结贴  问题点数:20 回复次数:1 
在ecplise中连接Mysql的问题
package test;

import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.*;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

import java.sql.*;
public class Login extends JFrame implements ActionListener {
    private  ResultSet rs;
    private  Statement st;
    private  Connection conn;
   
    private String host;
    private JPanel jp = new JPanel();
    private JLabel[] jl = { new JLabel("端口号"),  new JLabel("数据库服务器IP"),
            new JLabel("用户名"), new JLabel("密码") };
    private JLabel jl2 = new JLabel("");
    private JTextField[] jf = { new JTextField(), new JTextField(),
            new JTextField()};
    private JPasswordField jpwd = new JPasswordField();
    private JRadioButton[] jrb ={new JRadioButton("普通学生",true),
            new JRadioButton("管理人员")
    };
    private ButtonGroup bg = new ButtonGroup();//此类用于为一组按钮创建一个多斥(multiple-exclusion)作用域。使用相同的 ButtonGroup 对象创建一组按钮意味着“开启”其中一个按钮时,将关闭组中的其他所有按钮。
    private JButton jb1 = new JButton("登录");
    private JButton jb2 = new JButton("重置");
    public Login() {
        this.initialConnection();
        this.addListener();
        this.initialFrame();
        this.setFocus();
    }
   
    public void setFocus() {
        this.jf[2].requestFocus(true);
    }
   
    public void addListener() {
        this.jb1.addActionListener(this);
        this.jb2.addActionListener(this);
        this.jf[2].addActionListener(this);
        this.jpwd.addActionListener(this);
        this.jf[0].addActionListener(this);
        this.jf[1].addActionListener(this);
    }
   
    public void initialFrame() {
        jp.setLayout(null);
        for(int i=0; i<jl.length; i++) {
            jl[i].setBounds(20, 30+i*30, 100, 30);
            jp.add(jl[i]);
        }
        for(int j=0; j<jf.length; j++) {
            jf[j].setBounds(150, 30+j*30, 150, 30);
            jp.add(jf[j]);
        }
        jpwd.setBounds(150, 120, 150, 30);
        this.add(jpwd);
        jpwd.setEchoChar('*');
        this.bg.add(jrb[0]);
        this.bg.add(jrb[1]);
        jrb[0].setBounds(20, 180, 100, 30);
        jp.add(jrb[0]);
        jrb[1].setBounds(140, 180, 100, 30);
        jp.add(jrb[1]);
        jb1.setBounds(20, 240, 100, 30);
        jp.add(jb1);
        jb2.setBounds(140, 240, 100, 30);
        jp.add(jb2);
        jp.setBounds(20, 20, 500, 500);
        this.add(jp);
        this.setTitle("登录");
        Image image =new ImageIcon("").getImage();
        this.setIconImage(image);
        this.setResizable(false);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        
        int centerX =screenSize.width/2;
        int centerY = screenSize.height/2;
        int w=300;
        int h=320;
        this.setBounds(centerX-w/2,centerY-h/2-100,w,h);
        this.setVisible(true);
        this.jf[1].requestFocus(true);
        this.jf[1].setText("127.0.0.1");
        this.jf[0].setText("3306");
        
    }
   
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == this.jb1) {
           this.jl2.setText("正在验证,请稍后……");
           String hostadd = this.jf[1].getText().trim();
           if(hostadd.equals("")) {
               JOptionPane.showMessageDialog(this,"请输入端口号","错误",JOptionPane.ERROR_MESSAGE);
               return;
           }
           String hostport = this.jf[0].getText().trim();
           if(hostport.equals("")) {
               JOptionPane.showMessageDialog(this,"请输入端口号","错误",JOptionPane.ERROR_MESSAGE);
               return;
           }
           String username = this.jf[2].getText().trim();
           if(username.equals("")) {
               JOptionPane.showMessageDialog(this,"请输入用户名","错误",JOptionPane.ERROR_MESSAGE);
               return;
           }
           String password =this.jpwd.getText().trim();
           if(password.equals("")) {
               JOptionPane.showMessageDialog(this,"请输入密码","错误",JOptionPane.ERROR_MESSAGE);
               return;
           }
           try {
           int type = this.jrb[0].isSelected()? 0:1;
           if(type == 0) {
               String sql="select * from user_stu where "+
                        "stu_id='"+username+"' and pwd='"+password+"'";
                        rs=st.executeQuery(sql);
                        if(rs.next()){
                            new StudentChint(username,host);//创建学生客户短窗口
                            this.dispose();//关闭登陆窗口并释放资源
                        }
                        else{//弹出错误提示窗口
                            JOptionPane.showMessageDialog(this,"用户名或密码错误","错误",
                                                       JOptionPane.ERROR_MESSAGE);
                            jl2.setText("");
                        }
                        this.closeConn();//关闭连接,语句及结果集   
           }
           else{//教师登陆
                //创建sql语句并查询
                String sql="select coll_id from user_teacher where "+
                             "uid='"+username+"' and pwd='"+password+"'";
                rs=st.executeQuery(sql);
                if(rs.next()){
                    String coll_id=rs.getString(1);
                    new TeacherChint(coll_id,host);//创建教师客户端窗口
                    this.dispose();//关闭登陆窗口并释放资源
                }
                else{//弹出错误提示窗口
                    JOptionPane.showMessageDialog(this,"用户名或密码错误","错误",
                                               JOptionPane.ERROR_MESSAGE);
                    jl2.setText("");
                }
                this.closeConn();    //关闭连接,语句及结果集
            }
        }
        catch(SQLException ea){ea.printStackTrace();}
           
        }
        else if(e.getSource() == this.jf[0]) {
            this.jf[1].requestFocus(true);
        }
        else if(e.getSource() == this.jf[1]) {
            this.jf[2].requestFocus(true);
        }
        else if(e.getSource() == this.jf[2]) {
            this.jf[3].requestFocus(true);
        }
        else if(e.getSource() == this.jpwd) {
            this.jf[0].requestFocus(true);
        }
        else if(e.getSource() == jb1) {
            this.jf[2].setText("");
            this.jf[3].setText("");
        }

    }
   
    public  void  initialConnection()
    {
        try
        {//加载驱动,创建Connection及Statement
            Class.forName("com.mysql.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://"+host+"/test","root","520");
            st=conn.createStatement();
        }
        catch(SQLException e)
        {
            JOptionPane.showMessageDialog(this,"连接失败,请检查主机地址是否正确","错误",JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }
   
    public void closeConn()
    {
        try
        {
            if(rs!=null)
            {
                rs.close();
            }
            if(st!=null)
            {
                st.close();
            }
            if(conn!=null)
            {
                conn.close();
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
   
    public static void main(String args[])
    {
            //创建登陆窗体对象
            Login login=new Login();
    }

}

然后出现了下面的错误:
com.mysql.jdbc.exceptions. Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:357)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2482)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2519)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2304)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at test.Login.initialConnection(Login.java:184)
    at test.Login.<init>(Login.java:37)
    at test.Login.main(Login.java:224)
Caused by: null
    at (Native Method)
    at (Unknown Source)
    at (Unknown Source)
    at (Unknown Source)
    at (Unknown Source)
    at (Unknown Source)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:249)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:307)
    ... 17 more
搜索更多相关主题的帖子: private package public import 
2015-10-09 15:53
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:14 
数据库部分问题,检查sql语句吧

剑栈风樯各苦辛,别时冰雪到时春
2015-10-09 22:00
快速回复:在ecplise中连接Mysql的问题
数据加载中...
 
   



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

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