Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();//define a URLString
String url_string="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind";你要是用别的东西来驱动sqlserver2000,只要把第一句里的驱动程序换成别的就行。
这下知道默认端口是多少了吧。
然后就可以取得connection了。Conection conn=DriverManager.getConnection(url_string,username,password);
String url_string="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind";你要是用别的东西来驱动sqlserver2000,只要把第一句里的驱动程序换成别的就行。
这下知道默认端口是多少了吧。
然后就可以取得connection了。Conection conn=DriverManager.getConnection(url_string,username,password);
Driver drv = new com.microsoft.jdbc.sqlserver.SQLServerDriver();
Properties props = new Properties();
String m_ConnectString = "jdbc:microsoft:sqlserver://" + strHost + ":1433"; props.setProperty("user", strUserName);
props.setProperty("password", strPassWord); try {
m_Connection = drv.connect(m_ConnectString, props);
m_Connection.setCatalog (strDBName);
m_Statement = m_Connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
}
catch (SQLException e) {
e.printStackTrace();
} // try ... catch
}
=================一代过去,一代又来,地却永远长存。日头出来,日头落下,急归所出之地。
风往南刮,又往北转,不住的旋落,而且返回转行原道,江河都往海里转,海
却不满,江河从何处流,仍归何处。
放到你的webapps下的web-inf里
public ResultSet mssqlQuery(String Sql)
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection cnn = DriverManager.getConnection("jdbc.microsoft:sqlserver://127.0.0.1:1433;DatabaseName=testdb","sa","");
Statement stmt = cnn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
return stmt.executeQuery( Sql );
}
catch(Exception e)
{
return null;
}
}
没有合适的驱动。我装的是sql server 2000开发版 public ResultSet mssqlQuery(String Sql)
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection cnn = DriverManager.getConnection("jdbc.microsoft:sqlserver://creatxr:1433;DatabaseName=testdb","sa","");
Statement stmt = cnn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
return stmt.executeQuery( Sql );
}
catch(Exception e)
{
System.out.println("catch err: " + e.getMessage ());
return null;
}
}
String url_string="jdbc:jrun:sqlserver://localhost:1433:;DatabaseName=Northwind";
......
import java.util.*;
=================一代过去,一代又来,地却永远长存。日头出来,日头落下,急归所出之地。
风往南刮,又往北转,不住的旋落,而且返回转行原道,江河都往海里转,海
却不满,江河从何处流,仍归何处。
回复人: gcs925(鸟倦飞而知还) ( props.setProperty("user", strUserName);
props.setProperty("password", strPassWord);编译通不过啊。Properties pros = new Properties(); 编译通过了。没这个方法?
这是一个abstract类,需要你写一个子类,实现toHTML方法。
* 封装JDBC中常用操作
* @version : V 1.0
* @author :
*/package gcs.db;import java.sql.*;
import java.util.*;public abstract class GJdbc {
// protected property
protected Connection m_Connection;
protected Statement m_Statement;
protected PreparedStatement m_PrepStatement;
protected ResultSet m_ResultSet; // Eof & Bof
private boolean m_blnEmpty;
private boolean m_blnBof;
private boolean m_blnEof; private int m_intPageSize;
private int m_intPageNo;
private int m_intPageCount;
private int m_intRecordCount; // 分页显示中用到
private int m_intCursor; private int m_intFirstPage;
private int m_intPreviousPage;
private int m_intNextPage;
private int m_intLastPage;
public GJdbc () {
InitVariables();
} ///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// Public Method /**
* 连接数据库
* @param : strDBName, 使用jdbc:odbc方式时, 为系统dsn名字, 其它情况为数据库名
* @param : strUserName, 用户名
* @param : strPassWord, 密码
* @since : V 1.0
*/
public void Connect (String strHost, String strDBName, String strUserName, String strPassWord) {
Driver drv = new com.microsoft.jdbc.sqlserver.SQLServerDriver();
Properties props = new Properties();
String m_ConnectString = "jdbc:microsoft:sqlserver://" + strHost + ":1433"; props.setProperty("user", strUserName);
props.setProperty("password", strPassWord); try {
m_Connection = drv.connect(m_ConnectString, props);
m_Connection.setCatalog (strDBName);
m_Statement = m_Connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
}
catch (SQLException e) {
e.printStackTrace();
} // try ... catch
} /**
* 返回结果集ResultSet
* @param : strSql, sql语句
* @since : V 1.0
*/
public void executeQuery (String strSql) {
InitVariables(); try {
m_ResultSet = m_Statement.executeQuery(strSql);
setEmpty ();
}
catch (SQLException e) {
e.printStackTrace();
} // try ... catch
} /////////////////////////////////////////////////////////////////////////// /**
* 结果集是否为空
* @since : V 1.0
*/
public boolean isEmpty () {
return m_blnEmpty;
} /**
* Bof
* @since : V 1.0
*/
public boolean isBof () {
try {
if (isEmpty()) {
m_blnBof = true;
}
else if (m_intPageSize <= 0) {
m_blnBof = m_ResultSet.isBeforeFirst();
}
else {
m_blnBof = m_ResultSet.isBeforeFirst() || m_intCursor <= 0;
}
}
catch (SQLException e) {
e.printStackTrace();
} // try ... catch return m_blnBof;
} /**
* Eof
* @since : V 1.0
*/
public boolean isEof () {
try {
if (isEmpty()) {
m_blnEof = true;
}
else if (m_intPageSize <= 0) {
m_blnEof = m_ResultSet.isAfterLast();
}
else {
m_blnEof = m_ResultSet.isAfterLast() || m_intCursor >= getPageSize();
}
}
catch (SQLException e) {
e.printStackTrace();
} // try ... catch return m_blnEof;
} /////////////////////////////////////////////////////////////////////////// /**
* 设置页大小
* @param : intPageSize, 页大小
* @since : V 1.0
*/
public void setPageSize (int intPageSize) {
m_intPageSize = intPageSize; m_intPageCount = (int) Math.ceil(getRecordCount() * 1.0 / m_intPageSize);
} /**
* 返回页大小
* @since : V 1.0
*/
public int getPageSize () {
return m_intPageSize;
} /**
* 设置页号
* @param : intPageNo, 页号
* @since : V 1.0
*/
public void setPageNo (int intPageNo) {
// 如果未设置页大小, 设置默认页大小
if (m_intPageSize == -1) {
setPageSize(20);
} // 判断页号范围
if (intPageNo <= 0) {
m_intPageNo = 1;
}
else if (intPageNo > getPageCount()) {
m_intPageNo = getPageCount();
}
else {
m_intPageNo = intPageNo;
} // 第一页、上一页、下一页、最后一页
m_intFirstPage = 1;
m_intPreviousPage = m_intPageNo - 1;
m_intNextPage = m_intPageNo + 1;
m_intLastPage = m_intPageCount;
if (m_intPreviousPage <= 0) {
m_intPreviousPage = 1;
}
if (m_intNextPage > m_intPageCount) {
m_intNextPage = m_intPageCount;
} // 转到页
Move ((m_intPageNo - 1) * getPageSize() + 1);
} /**
* 返回页号
* @since : V 1.0
*/
public int getPageNo () {
return m_intPageNo;
} /**
* 返回结果集行数
* @since : V 1.0
*/
public int getRecordCount () {
int intPos = -1; if (m_intRecordCount == -1) {
try {
// 如果结果集为空, 返回0
if (isEmpty()) {
m_intRecordCount = 0;
}
else {
// 保存游标位置
intPos = m_ResultSet.getRow();
if (intPos <= 0) {
intPos = 1;
} // 返回行数
m_ResultSet.last();
m_intRecordCount = m_ResultSet.getRow(); // 恢复游标位置
m_ResultSet.absolute(intPos);
} // if ... else
}
catch (SQLException e) {
e.printStackTrace();
} // try ... catch
} return m_intRecordCount;
} /**
* 返回页数
* @since : V 1.0
*/
public int getPageCount () {
return m_intPageCount;
} /////////////////////////////////////////////////////////////////////////// /**
* getFirstPage
* @since : V 1.0
*/
public int getFirstPage () {
return m_intFirstPage;
} /**
* getPreviousPage
* @since : V 1.0
*/
public int getPreviousPage () {
return m_intPreviousPage;
} /**
* getNextPage
* @since : V 1.0
*/
public int getNextPage () {
return m_intNextPage;
} /**
* getLastPage
* @since : V 1.0
*/
public int getLastPage () {
return m_intLastPage;
}
/////////////////////////////////////////////////////////////////////////// /**
* Move
* @since : V 1.0
*/
public void Move (int intPos) {
try {
if (! isEmpty()) {
m_ResultSet.absolute (intPos);
toHTML();
}
}
catch (SQLException e) {
e.printStackTrace();
} // try ... catch
} /**
* MoveFirst
* @since : V 1.0
*/
public void MoveFirst () {
try {
if (! isEmpty()) {
m_ResultSet.first();
toHTML();
}
}
catch (SQLException e) {
e.printStackTrace();
} // try ... catch
} /**
* MovePrevious
* @since : V 1.0
*/
public void MovePrevious () {
try {
if (! isEmpty()) {
m_ResultSet.previous(); if (! m_ResultSet.isBeforeFirst()) {
m_intCursor--;
toHTML();
}
}
}
catch (SQLException e) {
e.printStackTrace();
} // try ... catch
} /**
* MoveNext
* @since : V 1.0
*/
public void MoveNext () {
try {
if (! isEmpty()) {
m_ResultSet.next();
if (! m_ResultSet.isAfterLast()) {
m_intCursor++;
toHTML();
}
}
}
catch (SQLException e) {
e.printStackTrace();
} // try ... catch
} /**
* MoveLast
* @since : V 1.0
*/
public void MoveLast () {
try {
if (! isEmpty()) {
m_ResultSet.last(); toHTML();
}
}
catch (SQLException e) {
e.printStackTrace();
} // try ... catch
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// Public Abstract Method /**
* toHTML
* @since : V 1.0
*/
public abstract void toHTML ();
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// Private Method /**
* InitVariables
* @since : V 1.0
*/
private void InitVariables () {
m_blnEmpty = false;
m_blnBof = false;
m_blnEof = false; m_intPageSize = -1;
m_intPageNo = -1;
m_intPageCount = -1;
m_intRecordCount = -1; m_intCursor = 0; m_intFirstPage = -1;
m_intPreviousPage = -1;
m_intNextPage = -1;
m_intLastPage = -1;
}
/**
* 结果集是否为空
* @since : V 1.0
*/
private void setEmpty () {
try {
m_blnEmpty = ! m_ResultSet.next();
if (! m_blnEmpty) {
m_ResultSet.beforeFirst ();
}
}
catch (SQLException e) {
e.printStackTrace();
} // try ... catch
}
}