当前位置: 技术问答>java相关
在JSP中通过JDBC访问数据库时,URL能不能直接指到某个数据库?
来源: 互联网 发布时间:2015-05-30
本文导语: 我现在只会如下使用: String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; String sConnStr = "jdbc:microsoft:sqlserver://" +dataserver+":1433;user="+username+";password=" +userpassword; ...... 然后访问时还...
我现在只会如下使用:
String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String sConnStr = "jdbc:microsoft:sqlserver://"
+dataserver+":1433;user="+username+";password=" +userpassword;
......
然后访问时还得用 USE 语句,如:
String sql = "USE DATABASEN " +
"INSERT INTO TABLEN ("+
.......
这样就很麻烦,可有良方,请多指教!THANKS!!!
String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String sConnStr = "jdbc:microsoft:sqlserver://"
+dataserver+":1433;user="+username+";password=" +userpassword;
......
然后访问时还得用 USE 语句,如:
String sql = "USE DATABASEN " +
"INSERT INTO TABLEN ("+
.......
这样就很麻烦,可有良方,请多指教!THANKS!!!
|
要像连接到具体的数据库,sConnStr应该是:
String sConnStr = "jdbc:microsoft:sqlserver://"
+dataserver+":1433;DatabaseName="+databasename+"user="+username+";password=" +userpassword
String sConnStr = "jdbc:microsoft:sqlserver://"
+dataserver+":1433;DatabaseName="+databasename+"user="+username+";password=" +userpassword
|
/**address: SQL Server的连接参数*/
String address = "jdbc:microsoft:sqlserver://192.168.7.6:1433";
/***user: 数据库用户名*/
String user="sa";
/**passwd: 用户密码*/
String passwd="";
/*数据库名*/
String database = "DMCADSQL";
DatabaseMetaData conMD = null;
java.sql.Statement stmt = null;
Connection con = null;
ResultSet sqlRst = null;
// try
// {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection(address,user,passwd);
conMD = con.getMetaData();
con.setCatalog(database);//指定数据库名称
stmt = con.createStatement();
// }catch(Exception fs) {
// out.print("Connection ERROR
");
// }
// out.print("Connection
");
try
{
stmt=con.createStatement();
sqlRst = stmt.executeQuery("select drawing_code,version,file_name from drawing");
out.print("stmt OK");
}catch(Exception gr)
{
out.print("stmt ERROR");
}
String address = "jdbc:microsoft:sqlserver://192.168.7.6:1433";
/***user: 数据库用户名*/
String user="sa";
/**passwd: 用户密码*/
String passwd="";
/*数据库名*/
String database = "DMCADSQL";
DatabaseMetaData conMD = null;
java.sql.Statement stmt = null;
Connection con = null;
ResultSet sqlRst = null;
// try
// {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection(address,user,passwd);
conMD = con.getMetaData();
con.setCatalog(database);//指定数据库名称
stmt = con.createStatement();
// }catch(Exception fs) {
// out.print("Connection ERROR
");
// }
// out.print("Connection
");
try
{
stmt=con.createStatement();
sqlRst = stmt.executeQuery("select drawing_code,version,file_name from drawing");
out.print("stmt OK");
}catch(Exception gr)
{
out.print("stmt ERROR");
}