日期:2014-05-18  浏览次数:20681 次

初学者问个连sqlserver问题
java连sqlserver要不要下载jar包?
有没有具体的例子可供参考,
谢谢高手

------解决方案--------------------
package beans;

import java.util.*;
import java.sql.*;
import javax.servlet.jsp.jstl.sql.*;

/**
* This class is a bean for executing SQL statements. It has three
* properties that can be set: connection, sqlValue and values.
* The connection and sqlValue properties must always be set before
* calling one of the execute methods. If the values property is
* set, the sqlValue property must be an SQL statement with question
* marks as placeholders for the value objects in the values
* property.
*
* @author Hans Bergsten, Gefion software <hans@gefionsoftware.com>
* @version 2.0
*/
public class SQLCommandBean {
private Connection conn;
private String sqlValue;
private List values;

/**
* Sets the Connection to use.
*/
public void setConnection(Connection conn) {
this.conn = conn;
}

/**
* Set the SQL string, possibly with question mark placeholders for
* values set by setValues().
*/
public void setSqlValue(String sqlValue) {
this.sqlValue = sqlValue;
}

/**
* Sets the values to use for the place holders in the SQL
* string. The List must contain one Object for each place holder,
* suitable for use with the PreparedStatement setObject() method.
*/
public void setValues(List values) {
this.values = values;
}

/**
* Executes the specified SQL string as a query and returns
* a Result object
*
* @return a javax.servlet.jsp.jstl.sql.Result object
* @exception SQLException
*/
public Result executeQuery() throws SQLException {
Result result = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
Statement stmt = null;
try {
if (values != null && values.size() > 0) {
// Use a PreparedStatement and set all values
pstmt = conn.prepareStatement(sqlValue);
setValues(pstmt, values);
rs = pstmt.executeQuery();
}
else {
// Use a regular Statement
stmt = conn.createStatement();
rs = stmt.executeQuery(sqlValue);
}
result = ResultSupport.toResult(rs);
}
finally {
if (rs != null) {
try {rs.close();} catch (SQLException e) {}
}
if (stmt != null) {
try {stmt.close();} catch (SQLException e) {}
}
if (pstmt != null) {
try {pstmt.close();} catch (SQLException e) {}
}
}
return result;
}

/**
* Executes the specified SQL string (any statement except SELECT, such
* as UPDATE, INSERT, DELETE or CREATE TABLE) and returns
* the number of rows affected by the statement, or 0 if none.
*
* @return the number of rows affected
* @exception SQLException
*/
public int executeUpdate() throws SQLException {
int noOfRows = 0;
ResultSet rs = null;
Pre