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 中为了兼容性而保留着,但我们不应该去使用他了。
2 年多之前 回复
hacker_wy
hacker_wy 回复jdukuangfeng: 驱动程序没有收到来自服务器的任何数据包,ip是不是有问题,你看看
2 年多之前 回复
jdukuangfeng
jdukuangfeng import java.util.*;
2 年多之前 回复
jdukuangfeng
jdukuangfeng 好的
2 年多之前 回复
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币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
数据库连接异常的解决过程
首先先简述一下遇到的问题,今天早上来了之后,网站突然报404的问题,不废话,直接看tomcat的日志吧,日志如下: ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the
连接数据库ClassNotFoundException的解决办法
前几天我在测试一个简单的java应用连接mysql数据库时,遇到了一个以前也遇到过的烦人问题。Class.forName(xx.xx.xx);抛出异常,此异常就是ClassNotFoundException。一开始我从网上找了一些方法,但是都无法成功,最后老师点明了错误。现在Class.forName加载出现ClassNotFoundException的几个原因归结一下。   首先得说明为什么出
解决mysql使用java web程序连接发生异常,数据库连接没问题的异常
解决mysql使用java web程序连接发生异常,数据库连接没问题我用本地web程序连接腾讯云服务器的数据库,会发生这种情况。异常内容如下05-22:47:08.709 [http-nio-8080-exec-6] ERROR com.alibaba.druid.pool.DruidDataSource - init datasource error com.mysql.jdbc.exceptio
关于javaWeb 连接数据库错误分析
问题一: A ResourcePool could not acquire a resource from its primary factory or sour 产生原因:数据库连接配置有误,如用户名是否正确,密码前后是否有多余的空格,URL和Driver是否完全匹配等; 解决办法:一一核对用户名、密码、URL、Driver等是否完全正确,并且检查数据库实例是否正确。 最后实在不行重启e
数据库连接异常
报错: java.sql.SQLException: Connections could not be acquired from the underlying database! caused by:     com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not ac
连接数据库时常见异常错误
连接数据库时,URL 里内含乾坤呀 如 db.url = jdbc:mysql://localhost:3306/picture? 解析: jdbc:mysql 协议 localhost IP 3306 端口 picture 数据库名 ?后面也能帮我们解决很多常见异常 InvalidConnectionAttributeException: The server time zon...
JSP页面与数据库连接中出现的中文乱码问题分析与解决
先打个比方,总司令发出一条命令是“每个士兵都把枪检查好”,这条命令需要 ‘总司令-->师长-->团长-->营长-->班长-->各个士兵’ ,这5步才能传到,其中就需要保证每个长的语言都要与总司令的语言一致,否则这条命令就会出错。语言一致,就相当于编码方式一致。     分析中文乱码问题的总原则是,找到中文的起始点(在哪输入的中文),一步一步分析检查,要保证每一步是否设置编码为gbk(或gb2
Mybatis连接数据库异常
本人在学习java Web开发,新手入门。请高手解答,谢谢 在之前调试的时候没有问题,在添加一部分代码后在原本没有出现问题的地方报null,代码看不出哪里出错。 数据库是mysql 下面是报错的详细: java.lang.NullPointerException at org.hjava.ec_Shop.util.SqlSessionFactoryUtil.getSqlSession(S
在myeclipse中使用数据库时提示空指针异常
在myeclipse中使用数据库时,一直获取不到Connection,驱动包已经放在了类路径中。但是在做单元测试时,是可以的。最后发现是要把驱动包放在tomcat的lib目录下。。
Visual studio 调试连接数据库异常解决方法
在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)      说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码