CSDN博客

img ziying

jdbc连接mssql

发表于2004/9/25 11:12:00  1082人阅读

package wap;

import java.io.*;
import java.sql.*;

public class jdbc {
  String strconn = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=setop";
  String strDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
  Connection conn = null ;
  Statement stmt = null;
  ResultSet rs = null;
  public java.sql.Connection jdbc(){    file://connection
    try{
     Class.forName(strDriver).newInstance();
  conn= DriverManager.getConnection(strconn,"sa","");
  stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);

 }
    catch(Exception e){
    e.printStackTrace();
    System.out.println("error");
    }
 return conn;
}

  public ResultSet executeQuery(String sql) {  file://select
  try {
   conn= DriverManager.getConnection(strconn,"sa","");
   stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
      rs = stmt.executeQuery(sql); 
    }catch(SQLException ex) {
   System.err.println("执行SQL语句出错select:" + ex.getMessage());
   }
    return rs;
}

   public ResultSet executeUpdate(String sql){   file://insert ,update
   try{
   conn= DriverManager.getConnection(strconn,"sa","");
   stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
   rs = stmt.executeQuery(sql);
   }
   catch(SQLException ex){
      System.err.println("执行SQL语句出错insert,update:" + ex.getMessage());
   }
      return rs;
   }

public void closeStmt(){   file://close statement
try{
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}

public void closeConn(){   file://close connection
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}

}

阅读全文
0 0

相关文章推荐

img
取 消
img