Java 代码连数据库报错
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 代码连数据库报错相关的知识,希望对你有一定的参考价值。
” com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown character set: 'utf8mb4' “
数据库的表采用的charset = UTF8, Netbeans 配置编码格式是UTF8. 同样的代码在另一台电脑上可以运行,不知道还有哪个环节可以修改
你要是想在你自己电脑上弄好的话,你检查下你页面中使用的编码是不是utf-8,可能你写错了,然后在mysql目录下的my.ini文件中,把default-character-set全部改成utf8,重新启动mysql服务。
希望能够帮到你。 参考技术B Unknown character set: 'utf8mb4' 未知的字符集
还有要看一下你的mysql有木有开启
还不行的话改成gbk的看看
再不行的话检查一下mysql的字符配置是不是一样 参考技术C Unknown character set: 'utf8mb4' ,连接字符串里的编码写错了,改成utf-8试试 参考技术D 错误信息说,这是个语法错误,不识别的字符集'utf8mb4' ,看看写的连接代码对不对,检查uri 第5个回答 2015-02-03 mysql驱动版本的问题。你把mysql的驱动版本改下试试
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>追问
我是采用最新下载的 jdbc 驱动, 直接用 jdbc 编程实现数据库访问的。
报错记录ZooKeeper报java.net.SocketException: Socket is not connected无法连接上
ZooKeeper版本:3.5.7
CentOS版本:7.9
由于我在学习ZooKeeper的阶段中,ZooKeeper都部署在虚拟机里了,心想着我就不配hosts了吧,反正虚拟机早晚要删掉。然后用java连接ZooKeeper就报错了,报:
ava.net.SocketException: Socket is not connected
at sun.nio.ch.Net.translateToSocketException(Net.java:123)
at sun.nio.ch.Net.translateException(Net.java:157)
at sun.nio.ch.Net.translateException(Net.java:163)
at sun.nio.ch.SocketAdaptor.shutdownInput(SocketAdaptor.java:401)
at org.apache.zookeeper.ClientCnxnSocketNIO.cleanup(ClientCnxnSocketNIO.java:198)
at org.apache.zookeeper.ClientCnxn$SendThread.cleanup(ClientCnxn.java:1338)
at org.apache.zookeeper.ClientCnxn$SendThread.cleanAndNotifyState(ClientCnxn.java:1276)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1254)
Caused by: java.nio.channels.NotYetConnectedException: null
at sun.nio.ch.SocketChannelImpl.shutdownInput(SocketChannelImpl.java:782)
at sun.nio.ch.SocketAdaptor.shutdownInput(SocketAdaptor.java:399)
... 4 common frames omitted
15:58:53.824 [main-SendThread(192.168.5.6:2181)] DEBUG org.apache.zookeeper.ClientCnxnSocketNIO - Ignoring exception during shutdown output
java.net.SocketException: Socket is not connected
at sun.nio.ch.Net.translateToSocketException(Net.java:123)
at sun.nio.ch.Net.translateException(Net.java:157)
at sun.nio.ch.Net.translateException(Net.java:163)
at sun.nio.ch.SocketAdaptor.shutdownOutput(SocketAdaptor.java:409)
at org.apache.zookeeper.ClientCnxnSocketNIO.cleanup(ClientCnxnSocketNIO.java:205)
at org.apache.zookeeper.ClientCnxn$SendThread.cleanup(ClientCnxn.java:1338)
at org.apache.zookeeper.ClientCnxn$SendThread.cleanAndNotifyState(ClientCnxn.java:1276)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1254)
Caused by: java.nio.channels.NotYetConnectedException: null
at sun.nio.ch.SocketChannelImpl.shutdownOutput(SocketChannelImpl.java:799)
at sun.nio.ch.SocketAdaptor.shutdownOutput(SocketAdaptor.java:407)
... 4 common frames omitted
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /dct
at org.apache.zookeeper.KeeperException.create(KeeperException.java:102)
at org.apache.zookeeper.KeeperException.create(KeeperException.java:54)
at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:1538)
at com.itdct.zoo.zk.ZkClient.create(ZkClient.java:38)
at com.itdct.zoo.zk.ZkClient.main(ZkClient.java:50)
解决方法是:
用Notepad++打开
C:\\Windows\\System32\\drivers\\etc\\hosts文件,添加ZooKeeper部署的服务器ip,然后保存。
如果发现hosts文件无法修改,按什么都没反应,大概率是hosts文件的权限是只读,只需要右键属性,去除其只读属性就行了。
保存的时候可能会提示是否用管理员权限重新打开,点击确定就行了。
如果还是报连接失败,可以按照以下步骤:
1、ping ZooKeeper服务器的ip地址,看看是否能够连通服务器
2、查看ZooKeeper服务器的防火墙,是否客户端端口被防火墙拦截了(可使用:service firewalld stop彻底关闭CentOS的防火墙)
3、确定ZooKeeper客户端的连接端口,是否是端口写错了,具体查看zoo.cfg中的clientPort=2181
4、查看ZooKeeper服务器的状态,查看其是否正常运行,查看方法:
{ZooKeeper安装路径}/bin/zkServer.sh status
如果出现Mode:{foller/leader/standalone}说明ZooKeeper启动是正常的,若出现报错,或者找不到服务,说明ZooKeeper服务端就没有启动成功
附连接代码:
package com.itdct.zoo.zk;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class ZkClient {
public String connectString = "ZK1:2181,ZK2:2181,ZK3:2181";
private ZooKeeper zooKeeper;
/**
* 初始化连接
*/
public void init() {
try {
zooKeeper = new ZooKeeper(connectString, 2000, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println("process: " + watchedEvent.getType());
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建节点
*/
public void create() {
try {
String nodeCreate = zooKeeper.create("/dct", "dct.txt".getBytes(StandardCharsets.UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("nodeCreate is " + nodeCreate);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ZkClient zkClient = new ZkClient();
zkClient.init();
zkClient.create();
}
}
以上是关于Java 代码连数据库报错的主要内容,如果未能解决你的问题,请参考以下文章
java hibernate修改语句执行没有报错,但数据库也没有改变是怎么回事
pb9,连oracle数据库,isql session执行sql就崩溃。