java.sql.SQLException:未选择数据库

编程入门 行业动态 更新时间:2024-10-10 23:18:44
本文介绍了java.sql.SQLException:未选择数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如上所述我有一个问题是选择数据库

as above i have a problem to "select the database"

我正在使用xampp,我在MySQL中创建了一个数据库,并将其命名为employees

i am using xampp, there i created a database in MySQL and named it "employees"

这是我的java代码:

This is my java code:

public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ //STEP 2: Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); //STEP 3: Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection("jdbc:mysql://localhost?user=root&password="); //STEP 4: Execute a query System.out.println("Creating statement..."); stmt = conn.createStatement(); String sql; sql = "SELECT id, first, last, age FROM employees"; ResultSet rs = stmt.executeQuery(sql);

如'sql'中所示,我尝试使用 FROM到达数据库员工

As seen in 'sql' I try to reach the database, using FROM employees

我是数据库编程新手。 我需要找到数据库的路径吗?如何以及在哪里可以找到它?

I am new to programming with databases. Do i need to find the path of the database? how and where can i find it?

推荐答案

更改连接字符串以使其连接到localhost上的正确数据库

Change your connection string to make it connect to the right database on localhost

conn = DriverManager.getConnection("jdbc:mysql://localhost/employees?user=root&password=");

或者你可以指定表的完整路径,即数据库.tablename :

Alternatively you can specify the "full" path to the tables, i.e. database.tablename:

sql = "SELECT id, first, last, age FROM employees.employees";

更多推荐

java.sql.SQLException:未选择数据库

本文发布于:2023-11-10 14:25:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575619.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   java   sql   SQLException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!