|
发表于 2004-6-16 14:16:06
|
显示全部楼层
[PHP]
package firstexample;
import java.sql.*;
import java.util.*;
import java.io.*;
public class FirstExample {
public static void main (String args []) {
Connection myConnection = null;
Statement myStatement = null;
try {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").
newInstance();
}
catch (ClassNotFoundException ex) {
}
catch (IllegalAccessException ex) {
}
catch (InstantiationException ex) {
}
// DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
// create a Connection object, and connect to the database
// jdbc:microsoft:sqlserver://localhost:2040
myConnection = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;User=saassword=;DatabaseName=XinWeiDB"
// ,"FreeDove","FreeDove"
);
// create a Statement object
myStatement = myConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
// create a ResultSet object, and populate it with the
// result of a SELECT statement that retrieves the
// user and sysdate variables from the database via
// the dual table - the executeQuery() method of the
// Statement object is used to perform the SELECT
ResultSet myResultSet = myStatement.executeQuery(
"SELECT * FROM test");
// retrieve the row from the ResultSet using the
System.out.println("OK.3");
// retrieve the user from the row in the ResultSet using the
myResultSet.getMetaData();
Vector v = new Vector();
myResultSet.absolute(14);
System.out.println("rel:" + myResultSet.getInt(1));
myResultSet.next();
// retrieve the sysdate from the row in the ResultSet using
// Timestamp currentDateTime = myResultSet.getTimestamp("sysdate");
// System.out.println("hi " + user +
// ", the current date and time is " + currentDateTime);
// close this ResultSet object using the close() method
myResultSet.afterLast();
myResultSet.previous();
myResultSet.close();
}catch (SQLException e){
System.out.println("Error code = " + e.getErrorCode());
System.out.println("Error message = " + e.getMessage());
}finally{
try {
// close the Statement object using the close() method
if (myStatement != null) {
myStatement.close();
}
// close the Connection object using the close() method
if (myConnection != null) {
myConnection.close();
}
} catch (SQLException e) {
System.out.println("Error code = " + e.getErrorCode());
System.out.println("Error message = " + e.getMessage());
}
}
} // end of main()
}
[/PHP]
这是找来的连接sql2000的例子,你把驱动变成mysql的就行了,其他的都一样 |
|