java与sql server 2005的相关操作

//本程序连接的是sql server 2005,与连接sql server 2000有点不同:driverName和URL都不同

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class create{
   public void getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
   {
    Statement stmt = null;
    Connection  dbConn;
    ResultSet rs;
        String user="sa";
        String pass="";
        String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=tempdb";
        try{
         Class.forName(driverName).newInstance();
          System.out.println("ok");
          dbConn = DriverManager.getConnection(URL,user,pass);
           System.out.println("Connection Successful!");
          stmt = dbConn.createStatement(); // 获得执行SQL语言的容器
          rs=dbConn.getMetaData().getTables(null, null, "testTb", null);
          if(rs.next()){
           System.out.println("testTb存在!咱现在把它删了吧!");
           stmt.executeUpdate("drop table testTb");
              System.out.println("回头再看看,没了吧?");
          }
          else{
           System.out.println("testTb不存在!咱现在就创建它吧!");
           stmt.executeUpdate("create table testTb(cola int,colb char(3),row int)");
              System.out.println("回头再看看,有了吧?");
          }
          } catch (SQLException e) {
          System.out.println("出错啦!");
         }
    }
 
  public static void main(String [] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
  {
   create conn=new create();
   conn.getConnection();
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值