Class.forName(props.getProperty("driver"));
String url = props.getProperty("url");
Connection con = DriverManager.getConnection(url, props);
// showWarnings(con.getWarnings());
initLanguage(con);
return con;
上面的Connection類的方法getWarnings檢索調用這個Connection對象的第一個警告.如果有多個可以用getNextWarning方法得到.
public void showWarnings(SQLWarning w) {
while (w != null) {
System.out.println(w.getMessage());
w = w.getNextWarning();
}
}
private void initLanguage(Connection con) throws SQLException {
Statement stmt = con.createStatement();
stmt.executeUpdate("set LANGUAGE 'us_english'");
stmt.close();
}
上面函數顯示了一個比較簡單的調用語句的方法.
這里是使用的屬性文件注冊.屬性文件如下:
--------------------------------------------------------------------
#屬性文件connection.properties.tmpl
#
# The URL of the server to which you want to connect
#
driver=net.sourceforge.jtds.jdbc.Driver
url=jdbc:jtds:sqlserver://localhost/dbname
USER=XXXX
PASSWORD=YYYY
SERVERNAME=localhost
PORTNUMBER=1433
DATABASENAME=dbname
XAEMULATION=true
#
# TDS Version
#
#
# Use this for MS SQL Server 6.5+
#
#TDS=4.2
#
# Use this for Sybase 10+
#
#TDS=5.0
#
# Use this for MS SQL Server 7.0+
#
#TDS=7.0
#
# Use this for MS SQL Server 2000 (default)
#
TDS=8.0
--------------------------------------------------------------------
讀入屬性文件如下:
String CONNECTION_PROPERTIES = "conf/connection.properties";
File propFile = new File(fileName);
if (!propFile.exists()) {
fail("Connection properties not found (" + propFile + ").");
}
try {
Properties props = new Properties();
props.load(new FileInputStream(propFile));
return props;
}
catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
這里是fail函數是使用了junit的fail方法,表示測試失敗.
String url = props.getProperty("url");
Connection con = DriverManager.getConnection(url, props);
// showWarnings(con.getWarnings());
initLanguage(con);
return con;
上面的Connection類的方法getWarnings檢索調用這個Connection對象的第一個警告.如果有多個可以用getNextWarning方法得到.
public void showWarnings(SQLWarning w) {
while (w != null) {
System.out.println(w.getMessage());
w = w.getNextWarning();
}
}
private void initLanguage(Connection con) throws SQLException {
Statement stmt = con.createStatement();
stmt.executeUpdate("set LANGUAGE 'us_english'");
stmt.close();
}
上面函數顯示了一個比較簡單的調用語句的方法.
這里是使用的屬性文件注冊.屬性文件如下:
--------------------------------------------------------------------
#屬性文件connection.properties.tmpl
#
# The URL of the server to which you want to connect
#
driver=net.sourceforge.jtds.jdbc.Driver
url=jdbc:jtds:sqlserver://localhost/dbname
USER=XXXX
PASSWORD=YYYY
SERVERNAME=localhost
PORTNUMBER=1433
DATABASENAME=dbname
XAEMULATION=true
#
# TDS Version
#
#
# Use this for MS SQL Server 6.5+
#
#TDS=4.2
#
# Use this for Sybase 10+
#
#TDS=5.0
#
# Use this for MS SQL Server 7.0+
#
#TDS=7.0
#
# Use this for MS SQL Server 2000 (default)
#
TDS=8.0
--------------------------------------------------------------------
讀入屬性文件如下:
String CONNECTION_PROPERTIES = "conf/connection.properties";
File propFile = new File(fileName);
if (!propFile.exists()) {
fail("Connection properties not found (" + propFile + ").");
}
try {
Properties props = new Properties();
props.load(new FileInputStream(propFile));
return props;
}
catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
這里是fail函數是使用了junit的fail方法,表示測試失敗.