当前位置:网站首页>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、解决
我的原因是这个数据库是我自己很久以前自己建的,用来练习的,不小心前面数据库前面多了个空格
把数据库的名字改一下就好了
单独看或许看不出来,比较一下就很明显了
边栏推荐
- At least 42 employees are infected with novel coronavirus! Nokia announces closure of telecom equipment plant in India
- Rongyun IM & RTC capabilities on new sites
- Jointly create a new chapter in cultural tourism | xinqidian signs a strategic cooperation agreement with Guohua cultural tourism
- C语言程序设计 | offsetof宏的讲解及其模拟实现
- 【STM32】看门狗模块
- Retinanet网络结构详解
- 【OpenCV 例程 300篇】241. 尺度不变特征变换(SIFT)
- Matlab 绘制 - 点和向量:向量加减的方法和源码
- Network device hard core technology insider firewall and security gateway (VII) virtualization artifact (Part 1)
- Focus on demand flow rather than idle people
猜你喜欢

【STM32】看门狗模块

IP address & subnet mask

函数相关知识

C language programming | explanation and Simulation of offsetof macro

Redis sentinel mode

Six relationships of UML class diagram, the best way to learn and understand

Database daily question --- day 22: last login

小波变换学习笔记

What is the org relationship mitigation strategy of Microsoft edge browser tracking prevention
![Thesis appreciation [iclr18] a neural language model combining syntax and vocabulary learning](/img/1c/5b9726b16f67dfc2016a0c2035baae.png)
Thesis appreciation [iclr18] a neural language model combining syntax and vocabulary learning
随机推荐
Redis cache penetration breakdown and avalanche
Postman下载、使用教程
一周年创作纪念日,冲吧少年郎
Sign up now | cloud native technology exchange meetup Guangzhou station has been opened, and I will meet you on August 6!
Focal Loss讲解
[STM32] watchdog module
Jerry's Bluetooth can only link back to the last device [article]
Iperf installation and use
[BuildRelease Management]Parabuild
One year anniversary of creation, Chongba young Lang
[CruiseControl]Build Result JSP
Network equipment hard core technology insider firewall and security gateway (V) security double repair method
scrollview、tableView嵌套解决方案
Recommended system model: DSSM twin tower model for embedded recall
Jerry caused other messages to accumulate in the message pool [article]
Srv6 debut
Branch and loop sentence exercises
Recommend a Hongmeng instant messaging software "fruit chat", which is a bit awesome!!
Use of postman
C language programming | single dog topic explanation

