码迷,mamicode.com
首页 > 数据库 > 详细

sqlserver 连接

时间:2014-12-25 11:30:47      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:localhost   sqlserver   package   import   public   

package demosql;
import java.sql.*;
public class test {
 static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
 static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=test";
 static final String USER = "su";
 static final String PWD = "zhang";
 static Statement sm = null;
 static Connection con = null; 
 public static void main(String args[]){
  
  try {   
   Class.forName(DRIVER);   
   con = DriverManager.getConnection(URL, USER,PWD);
   Statement sm = con.createStatement();
   int i = sm.executeUpdate("insert into 部门  values(4,‘yyy‘)");
   if(i == 1)
    System.out.println("Success");
   else
    System.out.println("Fail");
   
   } catch (ClassNotFoundException e) {   
    // TODO Auto-generated catch block  
    e.printStackTrace();
    System.out.println("not found");}
  catch (SQLException e) {   
   // TODO Auto-generated catch block  
   e.printStackTrace();  } 
  finally{
   if(sm != null)
    try {
     sm.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   if(con != null)
    try {
     con.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
  }
 // return con;
  }
 }



sqlserver 连接

标签:localhost   sqlserver   package   import   public   

原文地址:http://2172006.blog.51cto.com/2162006/1595750

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!