初学者问个连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;
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);
noOfRows = pstmt.executeUpdate();
}
else {
// Use a regular Statement
stmt = conn.createStatement();
noOfRows = stmt.executeUpdate(sqlValue);
}
}
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 noOfRows;
}
/**
* Calls setObject() method on the PreparedStatement for all
* objects in the values List.
*
* @param pstmt the PreparedStatement
* @param values a List with objects
* @exception SQLException
*/
private void setValues(PreparedStatement pstmt, List values)
throws SQLException {
for (int i = 0; i < values.size(); i++) {
Object v = values.get(i);
// Set the value using the method corresponding to the type.
// Note! Set methods are indexed from 1, so we add 1 to i
pstmt.setObject(i + 1, v);
}
}
}
package beans;
import java.sql.*;
import java.util.*;
import javax.servlet.jsp.jstl.sql.*;
public class TestSQLCommBean {
static String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
static String sConnStr =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs ";
public TestSQLCommBean() {
}
public static Connection GETConnection() {
Connection connect = null;
try {
Class.forName(sDBDriver);
} catch (java.lang.ClassNotFoundException e) {
System.err.println(e.getMessage());
}
try {
connect = DriverManager.getConnection(sConnStr, "sa ", "sa ");
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
return connect;
}
public static void main(String[] args) throws SQLException{
SQLCommandBean sqlCommandBean = new SQLCommandBean();
sqlCommandBean.setConnection(GETConnection());
StringBuffer sql = new StringBuffer();
sql.append( "SELECT * FROM Employee ")
.append( "WHERE emp_id = ? and job_id = ? ");
sqlCommandBean.setSqlValue(sql.toString());
List values = new ArrayList();
values.add( "PMA42628M ");
values.add(new Integer(13));
sqlCommandBean.setValues(values);
Result result = sqlCommandBean.executeQuery();
if (result == null || result.getRowCount() == 0) {
// User not found
System.out.println( "no search result!!! ");
}else{
System.out.println(result.getRows()[0].get( "fname ")+ " "+result.getRows()[0].get( "lname "));
}
}
}
[解决办法]
用数据源j2se直接支持
用驱动的话要下载驱动,各个数据库的驱动不一样
处理速度驱动较快
------解决方案--------------------
1、下载jdbc
2、baidu : jdbc sqlserver
[解决办法]
要下的 并且要把jar包放在工程lib 下面要是用eclipce开发的话把包 引近来就好了
安装的话不要 醅环境 不安装还要配下环境