【发布时间】:2011-05-26 05:06:28
【问题描述】:
我正在创建一个桌面应用程序,我必须在远程计算机上登录到 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
【问题讨论】:
-
只要您使用的登录名具有适当的权限,您就应该能够以相同的方式执行此操作,例如使用语句的 exeute() 方法。请粘贴您用于执行的代码以及您遇到的错误。
标签: java sql-server-2005 jdbc