2 jdukuangfeng jdukuangfeng 于 2016.04.06 20:44 提问

有一个连接数据库出现的异常 1C

出现异常
怎么回事啊

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(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:344)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2332)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at mysql.InsertIntDemo02.main(InsertIntDemo02.java:25)
Caused by: java.net.ConnectException: Connection refused: con

5个回答

hacker_wy
hacker_wy   2016.04.06 21:10

可以发代码来看看吗?我想看看代码

hacker_wy
hacker_wy 回复jdukuangfeng: org.gjt.mm.mysql.Driver 这个是 MySQL 老的 JDBC 驱动程序。最新的驱动应该使用 com.mysql.jdbc.Driver 这个。 当然了,org.gjt.mm.mysql.Driver 这个驱动在 MySQL 最新的 JDBC 中为了兼容性而保留着,但我们不应该去使用他了。
一年多之前 回复
hacker_wy
hacker_wy 回复jdukuangfeng: 驱动程序没有收到来自服务器的任何数据包,ip是不是有问题,你看看
一年多之前 回复
jdukuangfeng
jdukuangfeng import java.util.*;
一年多之前 回复
jdukuangfeng
jdukuangfeng 好的
一年多之前 回复
jdukuangfeng
jdukuangfeng   2016.04.06 21:17

import java.util.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ChangePwdTeacher extends JPanel implements ActionListener{
private String host;
private Connection conn;
private Statement stmt;
private ResultSet rs;
private JLabel[] jlArray={new JLabel("用户名"),new JLabel("原始密码"),new JLabel("新密码"),new JLabel("确认新密码")};
private JTextField jtf=new JTextField();
private JPasswordField[] jpfArray={new JPasswordField(),new JPasswordField(),new JPasswordField()};
private JButton[] jbArray={new JButton("确认"),new JButton("重置")};
public ChangePwdTeacher(String host){
this.host=host;
this.initialFrame();
this.addListener();
}
public void addListener(){
jtf.addActionListener(this);
jpfArray[0].addActionListener(this);
jpfArray[1].addActionListener(this);
jpfArray[2].addActionListener(this);
jbArray[0].addActionListener(this);
jbArray[1].addActionListener(this);
}
public void initialFrame(){
this.setLayout(null);
for(int i=0;i<jlArray.length;i++){
jlArray[i].setBounds(30,20+50*i,150,30);
this.add(jlArray[i]);
if(i==0){
jtf.setBounds(130,20+50*i,150,30);
this.add(jtf);
}
else{
jpfArray[i-1].setBounds(130,20+50*i,150,30);
this.add(jpfArray[i-1]);
}
}
jbArray[0].setBounds(40,230,100,30);this.add(jbArray[0]);
jbArray[1].setBounds(170,230,100,30);this.add(jbArray[1]);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jtf){
jpfArray[0].requestFocus(true);
}
else if(e.getSource()==jpfArray[0]){
jpfArray[1].requestFocus(true);
}
else if(e.getSource()==jpfArray[1]){
jpfArray[2].requestFocus(true);
}
else if(e.getSource()==jpfArray[2]){
jbArray[0].requestFocus(true);
}
else if(e.getSource()==jbArray[1]){
for(int i=0;i<jpfArray.length;i++){
jpfArray[i].setText("");}
jtf.setText("");
}
else if(e.getSource()==jbArray[0]){
String patternStr="[0-9a-zA-Z]{6,12}";
String user_name=jtf.getText().trim();
if(user_name.equals("")){
JOptionPane.showMessageDialog(this,"请输入用户名","错误",JOptionPane.ERROR_MESSAGE);
return;
}
String oldPwd=String.valueOf(jpfArray[0].getPassword()).trim();
if(oldPwd.equals("")){
JOptionPane.showMessageDialog(this,"请输入原始密码","错误",JOptionPane.ERROR_MESSAGE);
return;
}
String newPwd=String.valueOf(jpfArray[1].getPassword()).trim();
if(newPwd.equals("")){
JOptionPane.showMessageDialog(this,"请输入新密码","错误",JOptionPane.ERROR_MESSAGE);
return;
}
if(!newPwd.matches(patternStr)){
JOptionPane.showMessageDialog(this,"密码只能是6到12位的字母或数字","错误",JOptionPane.ERROR_MESSAGE);
return;
}
String newPwd1=String.valueOf(jpfArray[2].getPassword()).trim();
if(!newPwd.equals(newPwd1)){
JOptionPane.showMessageDialog(this,"确认密码与新密码不符","错误",JOptionPane.ERROR_MESSAGE);
return;
}
try{
this.initialConnection();//得到数据库连接
String sql="UPDATE user_teacher SET pwd=newPwd WHERE uid=user_name AND pwd=oldPwd";
int i=stmt.executeUpdate(sql);
if(i==0){
JOptionPane.showMessageDialog(this,"修改失败,请检查您的用户名或密码是否正确","错误",JOptionPane.ERROR_MESSAGE);
}
else if(i==1){
JOptionPane.showMessageDialog(this,"密码修改成功","提示",JOptionPane.ERROR_MESSAGE);
}
this.closeConn();
}
catch(Exception ea){ea.printStackTrace();}
}
}
public void initialConnection(){
try{
Class.forName("org.gjt.mm.mysql.Driver");
conn=DriverManager.getConnection("jdbc:mysql://10.227.152.200:3306/test","root","123456");
stmt=conn.createStatement();
}
catch(SQLException e)
{e.printStackTrace();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}

public void closeConn()
{
try
{
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
catch(SQLException e){
e.printStackTrace();
}
}
public static void main(String args[]){
ChangePwdTeacher cpt=new ChangePwdTeacher("10.227.152.200:3306");
JFrame jframe=new JFrame();
jframe.add(cpt);
jframe.setBounds(70,20,700,650);
jframe.setVisible(true);
}
}

jdukuangfeng
jdukuangfeng   2016.04.06 21:17

import java.util.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ChangePwdTeacher extends JPanel implements ActionListener{
private String host;
private Connection conn;
private Statement stmt;
private ResultSet rs;
private JLabel[] jlArray={new JLabel("用户名"),new JLabel("原始密码"),new JLabel("新密码"),new JLabel("确认新密码")};
private JTextField jtf=new JTextField();
private JPasswordField[] jpfArray={new JPasswordField(),new JPasswordField(),new JPasswordField()};
private JButton[] jbArray={new JButton("确认"),new JButton("重置")};
public ChangePwdTeacher(String host){
this.host=host;
this.initialFrame();
this.addListener();
}
public void addListener(){
jtf.addActionListener(this);
jpfArray[0].addActionListener(this);
jpfArray[1].addActionListener(this);
jpfArray[2].addActionListener(this);
jbArray[0].addActionListener(this);
jbArray[1].addActionListener(this);
}
public void initialFrame(){
this.setLayout(null);
for(int i=0;i<jlArray.length;i++){
jlArray[i].setBounds(30,20+50*i,150,30);
this.add(jlArray[i]);
if(i==0){
jtf.setBounds(130,20+50*i,150,30);
this.add(jtf);
}
else{
jpfArray[i-1].setBounds(130,20+50*i,150,30);
this.add(jpfArray[i-1]);
}
}
jbArray[0].setBounds(40,230,100,30);this.add(jbArray[0]);
jbArray[1].setBounds(170,230,100,30);this.add(jbArray[1]);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jtf){
jpfArray[0].requestFocus(true);
}
else if(e.getSource()==jpfArray[0]){
jpfArray[1].requestFocus(true);
}
else if(e.getSource()==jpfArray[1]){
jpfArray[2].requestFocus(true);
}
else if(e.getSource()==jpfArray[2]){
jbArray[0].requestFocus(true);
}
else if(e.getSource()==jbArray[1]){
for(int i=0;i<jpfArray.length;i++){
jpfArray[i].setText("");}
jtf.setText("");
}
else if(e.getSource()==jbArray[0]){
String patternStr="[0-9a-zA-Z]{6,12}";
String user_name=jtf.getText().trim();
if(user_name.equals("")){
JOptionPane.showMessageDialog(this,"请输入用户名","错误",JOptionPane.ERROR_MESSAGE);
return;
}
String oldPwd=String.valueOf(jpfArray[0].getPassword()).trim();
if(oldPwd.equals("")){
JOptionPane.showMessageDialog(this,"请输入原始密码","错误",JOptionPane.ERROR_MESSAGE);
return;
}
String newPwd=String.valueOf(jpfArray[1].getPassword()).trim();
if(newPwd.equals("")){
JOptionPane.showMessageDialog(this,"请输入新密码","错误",JOptionPane.ERROR_MESSAGE);
return;
}
if(!newPwd.matches(patternStr)){
JOptionPane.showMessageDialog(this,"密码只能是6到12位的字母或数字","错误",JOptionPane.ERROR_MESSAGE);
return;
}
String newPwd1=String.valueOf(jpfArray[2].getPassword()).trim();
if(!newPwd.equals(newPwd1)){
JOptionPane.showMessageDialog(this,"确认密码与新密码不符","错误",JOptionPane.ERROR_MESSAGE);
return;
}
try{
this.initialConnection();//得到数据库连接
String sql="UPDATE user_teacher SET pwd=newPwd WHERE uid=user_name AND pwd=oldPwd";
int i=stmt.executeUpdate(sql);
if(i==0){
JOptionPane.showMessageDialog(this,"修改失败,请检查您的用户名或密码是否正确","错误",JOptionPane.ERROR_MESSAGE);
}
else if(i==1){
JOptionPane.showMessageDialog(this,"密码修改成功","提示",JOptionPane.ERROR_MESSAGE);
}
this.closeConn();
}
catch(Exception ea){ea.printStackTrace();}
}
}
public void initialConnection(){
try{
Class.forName("org.gjt.mm.mysql.Driver");
conn=DriverManager.getConnection("jdbc:mysql://10.227.152.200:3306/test","root","123456");
stmt=conn.createStatement();
}
catch(SQLException e)
{e.printStackTrace();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}

public void closeConn()
{
try
{
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
catch(SQLException e){
e.printStackTrace();
}
}
public static void main(String args[]){
ChangePwdTeacher cpt=new ChangePwdTeacher("10.227.152.200:3306");
JFrame jframe=new JFrame();
jframe.add(cpt);
jframe.setBounds(70,20,700,650);
jframe.setVisible(true);
}
}

jdukuangfeng
jdukuangfeng   2016.04.06 21:18

import java.util.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ChangePwdTeacher extends JPanel implements ActionListener{
private String host;
private Connection conn;
private Statement stmt;
private ResultSet rs;
private JLabel[] jlArray={new JLabel("用户名"),new JLabel("原始密码"),new JLabel("新密码"),new JLabel("确认新密码")};
private JTextField jtf=new JTextField();
private JPasswordField[] jpfArray={new JPasswordField(),new JPasswordField(),new JPasswordField()};
private JButton[] jbArray={new JButton("确认"),new JButton("重置")};
public ChangePwdTeacher(String host){
this.host=host;
this.initialFrame();
this.addListener();
}
public void addListener(){
jtf.addActionListener(this);
jpfArray[0].addActionListener(this);
jpfArray[1].addActionListener(this);
jpfArray[2].addActionListener(this);
jbArray[0].addActionListener(this);
jbArray[1].addActionListener(this);
}
public void initialFrame(){
this.setLayout(null);
for(int i=0;i<jlArray.length;i++){
jlArray[i].setBounds(30,20+50*i,150,30);
this.add(jlArray[i]);
if(i==0){
jtf.setBounds(130,20+50*i,150,30);
this.add(jtf);
}
else{
jpfArray[i-1].setBounds(130,20+50*i,150,30);
this.add(jpfArray[i-1]);
}
}
jbArray[0].setBounds(40,230,100,30);this.add(jbArray[0]);
jbArray[1].setBounds(170,230,100,30);this.add(jbArray[1]);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jtf){
jpfArray[0].requestFocus(true);
}
else if(e.getSource()==jpfArray[0]){
jpfArray[1].requestFocus(true);
}
else if(e.getSource()==jpfArray[1]){
jpfArray[2].requestFocus(true);
}
else if(e.getSource()==jpfArray[2]){
jbArray[0].requestFocus(true);
}
else if(e.getSource()==jbArray[1]){
for(int i=0;i<jpfArray.length;i++){
jpfArray[i].setText("");}
jtf.setText("");
}
else if(e.getSource()==jbArray[0]){
String patternStr="[0-9a-zA-Z]{6,12}";
String user_name=jtf.getText().trim();
if(user_name.equals("")){
JOptionPane.showMessageDialog(this,"请输入用户名","错误",JOptionPane.ERROR_MESSAGE);
return;
}
String oldPwd=String.valueOf(jpfArray[0].getPassword()).trim();
if(oldPwd.equals("")){
JOptionPane.showMessageDialog(this,"请输入原始密码","错误",JOptionPane.ERROR_MESSAGE);
return;
}
String newPwd=String.valueOf(jpfArray[1].getPassword()).trim();
if(newPwd.equals("")){
JOptionPane.showMessageDialog(this,"请输入新密码","错误",JOptionPane.ERROR_MESSAGE);
return;
}
if(!newPwd.matches(patternStr)){
JOptionPane.showMessageDialog(this,"密码只能是6到12位的字母或数字","错误",JOptionPane.ERROR_MESSAGE);
return;
}
String newPwd1=String.valueOf(jpfArray[2].getPassword()).trim();
if(!newPwd.equals(newPwd1)){
JOptionPane.showMessageDialog(this,"确认密码与新密码不符","错误",JOptionPane.ERROR_MESSAGE);
return;
}
try{
this.initialConnection();//得到数据库连接
String sql="UPDATE user_teacher SET pwd=newPwd WHERE uid=user_name AND pwd=oldPwd";
int i=stmt.executeUpdate(sql);
if(i==0){
JOptionPane.showMessageDialog(this,"修改失败,请检查您的用户名或密码是否正确","错误",JOptionPane.ERROR_MESSAGE);
}
else if(i==1){
JOptionPane.showMessageDialog(this,"密码修改成功","提示",JOptionPane.ERROR_MESSAGE);
}
this.closeConn();
}
catch(Exception ea){ea.printStackTrace();}
}
}
public void initialConnection(){
try{
Class.forName("org.gjt.mm.mysql.Driver");
conn=DriverManager.getConnection("jdbc:mysql://10.227.152.200:3306/test","root","123456");
stmt=conn.createStatement();
}
catch(SQLException e)
{e.printStackTrace();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}

public void closeConn()
{
try
{
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
catch(SQLException e){
e.printStackTrace();
}
}
public static void main(String args[]){
ChangePwdTeacher cpt=new ChangePwdTeacher("10.227.152.200:3306");
JFrame jframe=new JFrame();
jframe.add(cpt);
jframe.setBounds(70,20,700,650);
jframe.setVisible(true);
}
}

ch717828
ch717828   2016.04.06 21:30

应该是连接异常。 你确保一下 用户名和密码是否正确。 并且数据库已经允许登录了。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!