当前位置:网站首页>Unknown database ‘xxxxx‘
Unknown database ‘xxxxx‘
2022-07-27 22:43:00 【大梦_几千秋】
1、问题描述
今天在回顾jdbc连接mysql的时间遇到了一个报错,Unknown database ‘xxxxx’
如图:
public class JDBCConnectMySQL {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/studysql?useSSL=false&useUnicode=true&characterEncoding=UTF-8";
static final String USER = "root";
static final String PASSWORD = "root";
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//1.注册驱动
Class.forName(JDBC_DRIVER);
// 提供jdbc的url
//2.创建数据库连接
connection = DriverManager.getConnection(DB_URL, USER, PASSWORD);
//3.创建statement实例
String sql = "select * from dept";
preparedStatement = connection.prepareStatement(sql);
//4.执行sql语句
resultSet = preparedStatement.executeQuery(sql);
//5.处理结果集
while (resultSet.next()){
int id = resultSet.getInt("id");
String deptName = resultSet.getString("name");
String loc = resultSet.getString("money");
System.out.print("id: " + id);
System.out.print("deptName: " + deptName);
System.out.println("loc: " + loc);
}
//6.关闭jdbc对象 先开后关
resultSet.close();
preparedStatement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
//安全起见,在finally里关闭资源,先判断是否为null
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
2、可能的原因
- 数据库名错误,仔细检查数据库名
- 检查Java代码,数据库配置的url中库名称与数据库是否一致
3、解决
我的原因是这个数据库是我自己很久以前自己建的,用来练习的,不小心前面数据库前面多了个空格
把数据库的名字改一下就好了
单独看或许看不出来,比较一下就很明显了
边栏推荐
- R language uses ggplot2 visualization: use ggpattern package to add custom stripe patterns, shadows, stripes, or other patterns or textures to the grouped bar graph
- Ford SUV "Mustang" officially went offline, safe and comfortable
- Swoole定时器
- Byte flybook Human Resource Kit three sides
- [STM32] watchdog module
- Wavelet transform learning notes
- [BuildRelease Management]Parabuild
- 文件系统挂载
- Recommended system model: DSSM twin tower model for embedded recall
- 推荐系统-精排模型:xDeepFM
猜你喜欢

Interface test practical project 02: read interface test documents and practice

Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始

小波变换学习笔记

node-red与TDengine交互

Safety detection risk
![[BuildRelease Management]Parabuild](/img/80/11c2b539c217ecd6ba55668d3e71e9.png)
[BuildRelease Management]Parabuild

红队大杀器 Behinder_v4.0(冰蝎4.0)

接口测试实战项目02:读懂接口测试文档,上手操练

Examples of application of JMeter in performance testing

110. SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe
随机推荐
Swoole定时器
一周年创作纪念日,冲吧少年郎
自用图床搭建教程
Use of swarm task task
C语言程序设计 | offsetof宏的讲解及其模拟实现
Postman download and use tutorial
计算属性的基本使用
0-1背包问题
Retinanet网络结构详解
Swoole内存-table详解
Ink wheel salon | Li Wenjie, Peking University: a graph database system for knowledge atlas application gstore
安全检测风险
Go 语言变量
Recommended system - offline recall: u2tag2i, ICF
Recommended system - indicators: CTR, CVR
At least 42 employees are infected with novel coronavirus! Nokia announces closure of telecom equipment plant in India
110. In depth introduction to sap ui5 fileuploader control - why do you need a hidden iframe
The most detailed summary of common English terms in the chip industry (quick grasp of graphics and text)
What is the reason for Chinese garbled code when dataworks transmits data to MySQL
Postman 的使用

