How to execute the sql with the native database api
Answered
I want to execute the sql,but now I can only generate a temp file and open it with the fileeditor.I don't know how to run it with the native database api?the datasource have been got.
Please sign in to leave a comment.
What are your use scenarios, maybe JDBC?
I wrote a plugin for mybatis.And I have opened the fileeditor with the sql,but I have no idea about auto execute it.
I think you can look at the manual execution button, see how does its Action source code execute
Hi,
See https://intellij-support.jetbrains.com/hc/en-us/community/posts/11067655904530-When-developing-the-idea-plug-in-how-to-execute-the-query-sql-and-get-the-query-result.
Karol Lewandowski I want to execute the sql in the sql fileeditor,just like auto click the run button.
What do you mean by auto click? Is there any existing similar feature? If yes, please show what you mean on a screenshot.
@Karol Lewandowski my plugin has implements to open the fileeditor by generated sql file.
but I have no idea to execute it.I want to make it execute like the database tool.
I'm not familiar with Database Tools and don't understand what feature you mean. Please refer to the existing feature.
Karol Lewandowski You mean I can only process the execution by myself?I have tried with this
but the console does't print the log.when I debug the system.out.println,I found it not work.
I have used the api,but can not execute with the nullpointerexception
Hi!
To execute the SQL, you need to:
1. Create or get existing session by using
com.intellij.database.console.session.DatabaseSessionManager#getSessions(com.intellij.openapi.project.Project, com.intellij.database.dataSource.LocalDataSource)
.2. Create request
com.intellij.database.datagrid.DataRequest.RawRequest
and overrideprocessRaw
method. Here you can get the connection from the second argument -com.intellij.database.dataSource.DatabaseConnectionCore#getRemoteConnection
.3. Send the request for execution
session.getMessageBus().getDataProducer().processRequest(request)
.