sqlserver 連接

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;
  }
 }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章