java数据库连接问题

虽不简单 2011-08-04 04:14:33
我想建个网站,连接多个服务器上的数据库,该怎么处理阿?
...全文
114 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
CaptainGan 2011-08-05
  • 打赏
  • 举报
回复
写个数据库连接类。
chang1984024 2011-08-05
  • 打赏
  • 举报
回复
写一个 可以传参数的 dao

public class StudentDAO {
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
Connection conn=null;
Statement state=null;
ResultSet rs = null;

public StudentDAO(String jx)
{
try
{
Class.forName(driver);
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName="+jx+"","sa","2008hongyu");
}
catch(Exception e)
{
e.printStackTrace();
}
}
Ade子夜 2011-08-05
  • 打赏
  • 举报
回复
顶!!!
EverWHL 2011-08-05
  • 打赏
  • 举报
回复
顶 楼上同胞
轻狂小疯 2011-08-05
  • 打赏
  • 举报
回复
同上!!
飓风zj 2011-08-04
  • 打赏
  • 举报
回复
多个数据源就可以了
softroad 2011-08-04
  • 打赏
  • 举报
回复

public static void main(String[] args) {

// Create a variable for the connection string.
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName=AdventureWorks;integratedSecurity=true;";

// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;

try {
// Establish the connection.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);

// Create and execute an SQL statement that returns some data.
String SQL = "SELECT TOP 10 * FROM Person.Contact";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);

// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString(4) + " " + rs.getString(6));
}
}

// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}

finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}

多写几个呗。
hlw_cn 2011-08-04
  • 打赏
  • 举报
回复
建立多个数据源,来回切换
amkaiom 2011-08-04
  • 打赏
  • 举报
回复
同1、2楼
huangdaxiang0726 2011-08-04
  • 打赏
  • 举报
回复
同上 同
gongzuo2011 2011-08-04
  • 打赏
  • 举报
回复
同意以上
shawn.bug 2011-08-04
  • 打赏
  • 举报
回复
那就写多个JDBC connection 被
qm4050 2011-08-04
  • 打赏
  • 举报
回复
建多个数据源

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧