如何使用JDBC在远程计算机上创建SQL Server2005数据库?

| 我正在创建一个桌面应用程序,在该应用程序中,我必须登录到远程计算机上的SQL Server 2005,并且必须创建数据库,用户等。通过使用jTDS,我能够创建与数据库的连接,并且能够执行\“ SELECT \”命令,但不能执行数据库命令,例如-创建数据库,创建用户等。 这是我的测试代码:
public class Test {

// JDBC driver name and database URL
   static final String JDBC_DRIVER = \"net.sourceforge.jtds.jdbc.Driver\";  
   static final String DB_URL = \"jdbc:jtds:sqlserver://10.96.11.31:1433;useNTLMv2=true;domain=myDomain\";
   public static void main(String[] args) {
       Connection conn = null;
       Statement stmt = null;
       try{
          //STEP 2: Register JDBC driver
          Class.forName(JDBC_DRIVER);

          //STEP 3: Open a connection
          System.out.println(\"Connecting to database...\");
          conn = DriverManager.getConnection(DB_URL);

          //STEP 4: Execute a query
          System.out.println(\"Creating database...\");
          stmt = conn.createStatement();

          String sql = \"CREATE DATABASE STUDENTS\";
          stmt.executeUpdate(sql);
          System.out.println(\"Database created successfully...\");
       }catch(SQLException se){
          //Handle errors for JDBC
          se.printStackTrace();
       }catch(Exception e){
          //Handle errors for Class.forName
          e.printStackTrace();
       }finally{
          //finally block used to close resources
          try{
             if(stmt!=null)
                stmt.close();
          }catch(SQLException se2){
          }// nothing we can do
          try{
             if(conn!=null)
                conn.close();
          }catch(SQLException se){
             se.printStackTrace();
          }//end finally try
       }//end try
       System.out.println(\"Goodbye!\");
    }//end main
    }//end JDBCExample
    
已邀请:
如果您需要通过JDBC创建数据库,则需要一个有权连接该服务器上的master数据库的用户。创建一个到master数据库的java.sql.Connection对象,然后简单地创建Statement并执行SQL: \“创建数据库MyDB \” 这个问题让我有些困惑,因此,如果我误解了,我深表歉意。     

要回复问题请先登录注册