急救 jsp或servlet什么连接服务器数据库——谢谢 jsp或servlet什么连接服务器数据库,数据库是sql server2008 ,老师只给了其地址,数据库名,数据库用户名,密码。我要什么访问上面的表。访问语句该什么写。 谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 以前写的代码package com.sias.util;import java.util.PropertyResourceBundle;import java.util.Enumeration;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.*;public class DB { private static String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //localhost改成你的服务器IP地址 private static String dbUrl = "jdbc:sqlserver://localhost:1433; DatabaseName=storagemanage"; private static String userName = "root"; //改成你的数据库名和密码 private static String passWord = "root"; private static java.sql.Connection conn = null; private static java.sql.Statement stmt = null; private static java.sql.ResultSet rs = null;// Connection conn = DriverManager.getConnection(url, "sa", "root"); static { try { Class.forName(dbDriver); } catch (java.lang.ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws java.sql.SQLException { if (conn == null || conn.isClosed()) conn = java.sql.DriverManager.getConnection(dbUrl, userName, passWord); return conn; } public ResultSet excuteQuery(String sqlStr) { if (sqlStr == null || sqlStr.equals("")) { return null; } try { this.getConnection(); this.stmt = this.conn.createStatement(); this.rs = this.stmt.executeQuery(sqlStr); } catch (java.sql.SQLException e) { e.printStackTrace(); } return this.rs; } public static boolean excuteUpdate(String sqlStr) { if (sqlStr == null || sqlStr.equals("")) { return false; } try { getConnection(); stmt = conn.createStatement(); stmt.executeUpdate(sqlStr); } catch (java.sql.SQLException e) { e.printStackTrace(); return false; } finally { try { if (stmt != null) { stmt.close(); } } catch (java.sql.SQLException e) { e.printStackTrace(); } try { if (conn != null) { conn.close(); } } catch (java.sql.SQLException e) { e.printStackTrace(); } } return true; } public boolean executeUpdates(String[] sqlStrs) { try { this.getConnection(); this.conn.setAutoCommit(false); this.stmt = conn.createStatement(); for (int i = 0; i < sqlStrs.length; i++) { this.stmt.addBatch(sqlStrs[i]); } stmt.executeBatch(); this.conn.commit(); this.conn.setAutoCommit(true); } catch (SQLException e) { try { if (conn != null) { conn.rollback(); } } catch (SQLException ex) { ex.printStackTrace(); } e.printStackTrace(); return false; } finally { try { if (stmt != null) stmt.close(); } catch (java.sql.SQLException e) { e.printStackTrace(); } try { if (conn != null) conn.close(); } catch (java.sql.SQLException e) { e.printStackTrace(); } } return true; } public void closeStmt() { try { if (this.stmt != null) { this.stmt.close(); } } catch (java.sql.SQLException e) { e.printStackTrace(); } } public static void closeConnection() { try { if (conn != null) { conn.close(); } } catch (java.sql.SQLException e) { e.printStackTrace(); } }} tomcat重启时抛:java.lang.ClassCastException,没道理,求解释 关于iterator?! getDefaultInstancede的区别 richfaces 的样式在哪里 孙卫勤的书的编译小错误 小菜鸟弱弱地向各位大虾问个dbcp连接池的问题!请大虾们进来看看! 为什么一用html:link就出这样的错误?? 如何实现啊?????? jsp怎么配置? 关于 iis6 tomcat6 jakarta 共享 80端口的问题 servlet调用与main调用 java模拟登陆,怎么才知道自己是登陆成功,我获取的是不是登陆成功后的session。
package com.sias.util;import java.util.PropertyResourceBundle;import java.util.Enumeration;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;import java.sql.*;public class DB {
private static String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//localhost改成你的服务器IP地址
private static String dbUrl = "jdbc:sqlserver://localhost:1433; DatabaseName=storagemanage";
private static String userName = "root";
//改成你的数据库名和密码
private static String passWord = "root";
private static java.sql.Connection conn = null;
private static java.sql.Statement stmt = null;
private static java.sql.ResultSet rs = null;// Connection conn = DriverManager.getConnection(url, "sa", "root");
static {
try {
Class.forName(dbDriver);
} catch (java.lang.ClassNotFoundException e) {
e.printStackTrace();
}
} public static Connection getConnection() throws java.sql.SQLException {
if (conn == null || conn.isClosed())
conn = java.sql.DriverManager.getConnection(dbUrl, userName,
passWord);
return conn;
} public ResultSet excuteQuery(String sqlStr) {
if (sqlStr == null || sqlStr.equals("")) {
return null;
}
try {
this.getConnection();
this.stmt = this.conn.createStatement();
this.rs = this.stmt.executeQuery(sqlStr);
} catch (java.sql.SQLException e) {
e.printStackTrace();
} return this.rs;
} public static boolean excuteUpdate(String sqlStr) {
if (sqlStr == null || sqlStr.equals("")) {
return false;
}
try {
getConnection();
stmt = conn.createStatement();
stmt.executeUpdate(sqlStr);
} catch (java.sql.SQLException e) {
e.printStackTrace();
return false;
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
return true;
} public boolean executeUpdates(String[] sqlStrs) {
try {
this.getConnection();
this.conn.setAutoCommit(false);
this.stmt = conn.createStatement();
for (int i = 0; i < sqlStrs.length; i++) {
this.stmt.addBatch(sqlStrs[i]);
}
stmt.executeBatch();
this.conn.commit();
this.conn.setAutoCommit(true);
} catch (SQLException e) {
try {
if (conn != null) {
conn.rollback();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
e.printStackTrace();
return false;
} finally {
try {
if (stmt != null)
stmt.close();
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
return true;
} public void closeStmt() {
try {
if (this.stmt != null) {
this.stmt.close();
}
} catch (java.sql.SQLException e) {
e.printStackTrace();
} } public static void closeConnection() {
try {
if (conn != null) {
conn.close();
}
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
}