我正在创建一个桌面应用程序,在该应用程序中,我必须登录到远程计算机上的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