爱玺玺

爱玺玺的生活日记本。wx:lb87626

java对数据库sqlserver2005进行操作

package com.jdbcodbc;


import java.net.ConnectException;

import java.sql.*;

public class Test1 {

    public static void main(String args[]) {

    Statement s=null;

    String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";  //驱动包连接字符串

    String dbURL="jdbc:sqlserver://localhost:1433;integratedSecurity=true;DatabaseName=BBB";

    //数据库连接字符串(注意端口书写,默认是1433,可以在SQLServer2005-》“配置工具”-》SQL Server


    //Configuration Manager-》“SQL Server2005网络配置”-》“MSSQLSERVER的协议”-》“TCP/IP(需开


    //启)”-》“TCP/IP属性”-》“IP地址”-》“TCP端口”中查看)

    Connection dbConn; //创建数据库连接对象

      try{

       Class.forName(driverName);

       dbConn=DriverManager.getConnection(dbURL,"sa", "sa");

       s=dbConn.createStatement();

       s.executeUpdate("insert into dept values(50,'保安部','德阳')");

      }catch(Exception e) //捕获异常

      {

      e.printStackTrace(); 

      }

      finally{

      try {

      s.close();

          dbConn.close();

} catch (Exception e2) {

e2.printStackTrace(); 

}

      

      }

    }

}


注意还要打开数据库的相关权限


发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5