How can I execute the following query against my database instance using JDBC, Everything I am trying is returning "no result set" errors.
com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.
What do I do when I'm not really looking data back as such?
.sql file
DECLARE @name VARCHAR(50) -- database name
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name
DECLARE @fileFolder VARCHAR(20) -- used for file name
-- specify database backup directory
SET @path = 'C:\SQLBackups\'
-- specify filename format
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb') -- exclude these databases
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
BACKUP DATABASE @name TO DISK = @fileName
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor
My code for executing said query (so far)
public static void connect(String instance, String saPassword, String query) {
Connection conn = null;
query = "DECLARE @name VARCHAR(50) -- database name " +
"DECLARE @path VARCHAR(256) -- path for backup files " +
"DECLARE @fileName VARCHAR(256) -- filename for backup " +
"DECLARE @fileDate VARCHAR(20) -- used for file name " +
"-- specify database backup directory " +
"SET @path = 'C:\\SQLBackups\\' " +
"-- specify filename format " +
"SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) " +
"DECLARE db_cursor CURSOR FOR " +
"SELECT name " +
"FROM master.dbo.sysdatabases " +
"WHERE name NOT IN ('master','model','msdb','tempdb') -- exclude these databases " +
"OPEN db_cursor " +
"FETCH NEXT FROM db_cursor INTO @name " +
"WHILE @@FETCH_STATUS = 0 " +
"BEGIN " +
" SET @fileName = @path + @name + '_' + @fileDate + '.BAK' " +
" BACKUP DATABASE @name TO DISK = @fileName " +
" FETCH NEXT FROM db_cursor INTO @name " +
"END " +
"CLOSE db_cursor " +
"DEALLOCATE db_cursor ";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
String dbURL = "jdbc:sqlserver://10.0.0.0\\"+ instance;
String user = "user";
String pass = saPassword;
conn = DriverManager.getConnection(dbURL, user, pass);
Statement stmt = conn.createStatement();
stmt.executeQuery(query);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
I can execute my query just fine in SSMS, is the conversion of the .sql text into the string causing issues here? I'm not too bothered about a result here as it should be creating a lot of .bak files on the server (which does happen when I execute it via SSMS, I changed around the query with grabbing a simple SELECT resultset which works just fine, so I'm a tad lost now.
overwriting the (query) in the method is just for testing purposes, once I get the .bak files landing on the remote server directory I will tidy it up, credentials masked for obvious reasons.
Thanks
In your java code call execute()
instead of executeQuery()
. The latter supposes that your query needs to return a result set, but the former does not assume that.
So, instead of:
stmt.executeQuery(query);
call:
stmt.execute(query);
Your code is NOT readable (and therefore hard to maintain) as you are mixing both SQL & Java languages together, so I strongly recommend to use callableStatement
, you can look here for a simple example. In simple terms, you need to move all your SQL code to a procedure in the database and then callableStatement.execute()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.