1.已知:
a.我有一个数据库,在VC或VB里用ADO的连接串是:
Provider=SQLOLEDB.1;
Persist Security Info=False;
User ID=sa;pwd=;Initial Catalog=gateway;
Data Source=192.168.10.102
b.在数据库gateway有一个表:test
2.问题
a.我如何得到一个conn:连接192.168.10.102的数据库gateway
b.我如何得到一个rs:查select * from test
在下先表示谢了!
a.我有一个数据库,在VC或VB里用ADO的连接串是:
Provider=SQLOLEDB.1;
Persist Security Info=False;
User ID=sa;pwd=;Initial Catalog=gateway;
Data Source=192.168.10.102
b.在数据库gateway有一个表:test
2.问题
a.我如何得到一个conn:连接192.168.10.102的数据库gateway
b.我如何得到一个rs:查select * from test
在下先表示谢了!
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection("jdbc:odbc:" + "LocalServer");
isConnected = true;
} catch (ClassNotFoundException ce) {
//Common.msg("failed to loda jdbc/odbc driver.");
} catch (SQLException e) {
//Common.msg("unable to connect");
//e.printStackTrace();
}
//以上为本地连接 Statement statement;
ResultSet resultset;
statement = connection.createStatement();
resultset = statement.executeQuery("select * from institute");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver);
String url="jdbc:microsoft:sqlserver://192.168.10.102:1433;DatabaseName=gateway";
String user="sa";
String password="";
Connection conn= java.sql.DriverManager.getConnection(url,user,password);
catch (ClassNotFoundException ce) {}
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver);
String url="jdbc:microsoft:sqlserver://192.168.10.102:1433;DatabaseName=gateway";
String user="sa";
String password="";
Connection conn= java.sql.DriverManager.getConnection(url,user,password);
}
catch (ClassNotFoundException ce)
{//得到信息}
--------------------------------------
statement = connection.createStatement();
ResultSet rs=stmt.executeQuery(select * from test);
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver);
String url="jdbc:microsoft:sqlserver://192.168.10.102:1433;DatabaseName=gateway";
String user="sa";
String password="";
Connection conn= java.sql.DriverManager.getConnection(url,user,password);
}
catch (ClassNotFoundException ce){}statement = connection.createStatement();
ResultSet rs=stmt.executeQuery(select * from test);
while(rs.next()){
rs.getString("字段1");
.....
}
public static Connection getConnection() throws java.sql.SQLException {
Connection conn = null;
Context ctx = null;
DataSource ds = null;
try {
ctx = new InitialContext();
ds = (DataSource) ctx.lookup("MSSQLJNDI");
conn = ds.getConnection();
} catch (Exception e) {
System.out.println(e.getMessage());
throw new SQLException("Cannot get connection!" + e.getMessage());
} if (conn == null)
throw new SQLException("Cannot get connection!");
return conn;
}}