Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Create and handle several DBs in SQL Server (2017) from Java

Rolin Azmitia:

I'm developing a desktop app to organize different events, thus creating a DB for each event. So far, I've managed to create a DB with whatever name the user wants, using a simple GUI.

However, I can't create tables nor columns for said database, even though it's exactly the same syntax I use in SQL Server Manager.

My code so far:

public static void creDB(String db_name, String table_name){

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try {
conn = DriverManager.getConnection(connectionUrl);

String SQL = "CREATE DATABASE " + db_name;
stmt = conn.createStatement();
int result = stmt.executeUpdate(SQL);

String SQL3 = "USE " + db_name;
boolean ree = stmt.execute(SQL3);
String SQL4 = "GO";
boolean rr = stmt.execute(SQL4);

if (result == 0){
System.out.println("Se insertó :D!");
int res = stmt.executeUpdate(SQL2);
if (res == 0)
System.out.println("GRACIAS DIOS");
System.out.println("Raios shico");

}catch (Exception e) {e.printStackTrace();}
finally {
if (rs != null) try {rs.close();} catch (Exception e) {e.printStackTrace();}
if (stmt != null) try {stmt.close();} catch (Exception e) {e.printStackTrace();}
if (conn != null) try {conn.close();} catch (Exception e) {e.printStackTrace();}

The error I get is when I try to actually use the DB, using the use [DB name] go; I tried already using that same syntax in one single SQL statement, however it didn't work, so I tried doing it separately and got this error: Could not find stored procedure 'GO'.

I know the code above looks like a mess, and it is, but it's just for testing purposes since I'm new to doing DB-related projects with Java; I mixed-matched some of the concepts of this site, which were successful up until the creation of the tables.

I know there's a better way of managing several databases, but as I said, I'm just starting so any advice would be greatly appreciated.

Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered

This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Create and handle several DBs in SQL Server (2017) from Java


Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription