跪求一个jsp+javabean连接sql server 2005的源代码~
2个回答
展开全部
package cn.afss.common.web.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
public class TestConnSql2k5Bean {
private static final Logger logger =
Logger.getLogger(TestConnSql2k5Bean.class);
private Connection conn = null;
public TestConnSql2k5Bean() {
super();
}
public void getConnection() {
try {
Class
.forName("com.microsoft.SQL Server.jdbc.SQL ServerDriver")
.newInstance();
String URL = "jdbc:SQL Server://localhost:1433;DatabaseName=tempdb";
String USER = "sa"; // 根据你自己设置的数据库连接用户进行设置
String PASSWORD = "123456"; // 根据你自己设置的数据库连接密码进行设置
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (java.lang.ClassNotFoundException ce) {
logger.error("Get Connection error:", ce);
} catch (java.sql.SQLException se) {
logger.error("Get Connection error:", se);
} catch (Exception e) {
logger.error("Get Connection error:", e);
}
}
public void testConnection() {
if (conn == null)
this.getConnection();
try {
String sql = "SELECT * FROM TABLE_1";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
logger.debug(rs.getString("ID"));
logger.debug(rs.getString("NAME"));
logger.debug(rs.getString("EMAIL"));
}
rs.close();
stmt.close();
} catch (SQLException e) {
logger.error(e.getMessage(), e);
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
}
}
}
public static void main(String[] args) {
TestConnSql2k5Bean bean = new TestConnSql2k5Bean();
bean.testConnection();
}
}
三、SQL Server2000和2005的连接代码区别
写连接代码时需要注意2000和2005的不同:
1、JSP JDBC连接SQL Server2000
Class.forName("com.microsoft.jdbc.SQL Server.SQL ServerDriver").newInstance();
URL = "jdbc:microsoft:SQL Server://localhost:1433;DatabaseName=tempdb";
2、JSP JDBC连接SQL Server2005
Class.forName("com.microsoft.SQL Server.jdbc.SQL ServerDriver").newInstance();
URL = "jdbc:SQL Server://localhost:1433;DatabaseName=tempdb";
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
public class TestConnSql2k5Bean {
private static final Logger logger =
Logger.getLogger(TestConnSql2k5Bean.class);
private Connection conn = null;
public TestConnSql2k5Bean() {
super();
}
public void getConnection() {
try {
Class
.forName("com.microsoft.SQL Server.jdbc.SQL ServerDriver")
.newInstance();
String URL = "jdbc:SQL Server://localhost:1433;DatabaseName=tempdb";
String USER = "sa"; // 根据你自己设置的数据库连接用户进行设置
String PASSWORD = "123456"; // 根据你自己设置的数据库连接密码进行设置
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (java.lang.ClassNotFoundException ce) {
logger.error("Get Connection error:", ce);
} catch (java.sql.SQLException se) {
logger.error("Get Connection error:", se);
} catch (Exception e) {
logger.error("Get Connection error:", e);
}
}
public void testConnection() {
if (conn == null)
this.getConnection();
try {
String sql = "SELECT * FROM TABLE_1";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
logger.debug(rs.getString("ID"));
logger.debug(rs.getString("NAME"));
logger.debug(rs.getString("EMAIL"));
}
rs.close();
stmt.close();
} catch (SQLException e) {
logger.error(e.getMessage(), e);
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
}
}
}
public static void main(String[] args) {
TestConnSql2k5Bean bean = new TestConnSql2k5Bean();
bean.testConnection();
}
}
三、SQL Server2000和2005的连接代码区别
写连接代码时需要注意2000和2005的不同:
1、JSP JDBC连接SQL Server2000
Class.forName("com.microsoft.jdbc.SQL Server.SQL ServerDriver").newInstance();
URL = "jdbc:microsoft:SQL Server://localhost:1433;DatabaseName=tempdb";
2、JSP JDBC连接SQL Server2005
Class.forName("com.microsoft.SQL Server.jdbc.SQL ServerDriver").newInstance();
URL = "jdbc:SQL Server://localhost:1433;DatabaseName=tempdb";
2011-09-21
展开全部
子俸粟
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询