简体   繁体   English

在不选择java中的数据库的情况下执行Mysql查询

[英]Executing a Mysql query without selecting database in java

I want to execute a MySQL query without selecting a database in Java. 我想在不选择Java数据库的情况下执行MySQL查询。 I am able to do so directly in the MySQL console, like: 我可以直接在MySQL控制台中这样做,例如:

CREATE TEMPORARY TABLE  temptable
As(select REPLACE(a.keywordSupplied,"www.","") as keywordSupplied
from db1.table1 
;

I am able to execute the above query directly after logging into the MySQL console.I don't make use of the use statement here 我可以在登录MySQL控制台后直接执行上述查询。我没有在这里use语句

use databasename;

I need to do this as I will be taking data from two different databases in a single query. 我需要这样做,因为我将在一个查询中从两个不同的数据库中获取数据。 According to http://www.tutorialspoint.com/jdbc/jdbc-create-database.htm I gave DB URL as jdbc:mysql://localhost/ 根据http://www.tutorialspoint.com/jdbc/jdbc-create-database.htm我将DB URL作为jdbc:mysql:// localhost /

but it throws an Exception 但它抛出一个Exception

java.sql.SQLException: No database selected.

Screenshot for the same in mysql console 在mysql控制台中相同的屏幕截图 在此输入图像描述

Any solutions to make it work? 任何使其有效的解决方案?

Instead of 代替

DriverManager.getConnection("jdbc:mysql://localhost/", USER, PASS);

Use: 采用:

DriverManager.getConnection("jdbc:mysql://localhost/db1", USER, PASS);//specify the db within URL itself.btw dont you have port in url?

In the tutorial you are following, they issue a CREATE DATABASE statement. 在您正在阅读的教程中,它们发出CREATE DATABASE语句。 This statement is one of the very few statements that you can issue outside of the context of a database (the only other one I can think about is CREATE USER ). 这个语句是您可以在数据库上下文之外发出的极少数语句之一(我能想到的另一个语句是CREATE USER )。

Even a TEMPORARY table must be attached to a database. 甚至TEMPORARY表也​​必须附加到数据库。

You must choose an arbitrary database for this operation. 必须为此操作选择任意数据库。 You probably do not care which one in particular (just make sure you have write permissions on the one you select :) 你可能不关心特别是哪一个(只是确保你对你选择的那个有写权限:)

If you really do not want to provide a database at connection time, you can specify the target database in the SQL statement: CREATE TEMPORARY TABLE [database].temptable . 如果您确实不想在连接时提供数据库,可以在SQL语句中指定目标数据库: CREATE TEMPORARY TABLE [database].temptable

Try this code: 试试这段代码:

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
static final String DB_URL = "jdbc:mysql://localhost:3306/";
static final String USER = "root";
static final String PASS = "";
Class.forName(JDBC_DRIVER);
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println(conn);
conn.setCatalog("mysql");
PreparedStatement stmt1 = conn.prepareStatement("CREATE TEMPORARY TABLE  temptable (select test.a.id as id, students.a.id as sid from test.a join students.a on test.a.id = students.a.id)");
stmt1.execute();
stmt1 = conn.prepareStatement("select * from temptable");
ResultSet set1 = stmt1.executeQuery();
while(set1.next()){
    System.out.println(set1.getString(1));  
}

Now you have the connection with your database server execute your query. 现在,您与数据库服务器建立了连接,执行查询。 I hope your query will be execute without any problem. 我希望您的查询将毫无问题地执行。

看来,当你“select * from test.demo;”时,它实际上选择了'test'数据库,并在'demo'表中进行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM