我正在創建一個桌面應用程序,在該應用程序中,我必須登錄到遠程計算機上的SQL Server 2005,並且必須創建數據庫,用戶等。通過使用jTDS,我能夠創建到數據庫的連接以及能夠執行「SELECT」 commnads,但不能執行命令數據庫喜歡 - 創建數據庫,創建用戶等如何在使用JDBC的遠程機器上創建SQL Server2005數據庫?
這裏是我的測試代碼:
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
只要您使用的登錄名具有適當的權限,您應該能夠以相同的方式執行此操作,例如使用Statement的exeute()方法。請粘貼您用來執行的代碼和您收到的錯誤。 – Jon 2011-05-26 07:42:14