ajax经典

[b]实现FAQ动态加载用的SQLSERVER2005
index.jsp

<%@ page contentType="text/plain; charset=UTF-8"%>
<%@page import="java.sql.*,ajax.db.DBUtils"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>动态加载的FAQ</title>
<script type="text/javascript">

var xmlHttp;

var currFaqId;   //用于保存当前想要获取的FAQ编号
//用于创建XMLHttpRequest对象

function createXmlHttp(){

if(window.XMLHttpRequest){

xmlHttp=new XMLHttpRequest();

}else{

xmlHttp=new ActiveXObjext("Microsoft.XMLHTTP");
}
}
//获取FAQ信息的调用函数
function loadFAQ(faqId){
currFaqId=faqId;

var currFaqDetail=getFaqDetailDiv(faqId);

if(currFaqDetail.style.display=="none"){

currFaqDetail.style.display="block"

if(currFaqDetail.innerHTML==""){

createXmlHttp();

xmlHttp.onreadystatechange=loadFAQCallback;

xmlHttp.open("GET","read_faq.jsp?faqId="+faqId,true);

xmlHttp.send(null);

}

}else{

currFaqDetail.style.display="none";
}
}




//获取FAQ信息的回调函数
function loadFAQCallback() {

if(xmlHttp.readyState==4){

getFaqDetailDiv(currFaqId).innerHTML=xmlHttp.responseText;
}
}




//根据faqId取得对应的div节点

function getFaqDetailDiv(faqId){

return document.getElementById("faqDetail"+faqId);
}


</script>

</script>
</head>

<body>
<h1>
FAQ(常见问题)
</h1>
<%
String sql = "select id,faq from faq order by id asc";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DBUtils.getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
%>
<div>
<a href="#" onclick="loadFAQ(<%=rs.getInt(1)%>);return false;"> <%=rs.getString(2)%>
</a>
</div>
<div id="faqDetail<%=rs.getInt(1)%>" style="display: none"></div>
<%
}
} catch (SQLException e) {
System.out.println(e.toString());
} finally {
DBUtils.close(rs);
DBUtils.close(pstmt);
DBUtils.close(conn);
}
%>
</body>
</html>
read_faq.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@page import="java.sql.*,ajax.db.DBUtils"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
 
  <body>
  <%
  out.clear();
  String faqIdStr=request.getParameter("faqId");
  String faqDetail=null;
  if(faqIdStr!=null){
  int faqId=Integer.parseInt(faqIdStr);
  String sql="select detail from faq where id=?";
  Connection conn=null;
  PreparedStatement pstmt=null;
  ResultSet rs=null;
  try{
  conn=DBUtils.getConnection();
  pstmt=conn.prepareStatement(sql);
  pstmt.setInt(1,faqId);
  rs=pstmt.executeQuery();
  if(rs.next()){
  faqDetail=rs.getString(1);
  }
  }catch(SQLException e){
  System.out.println(e.toString());
  }finally{
  DBUtils.close(rs);
  DBUtils.close(pstmt);
  DBUtils.close(conn);
  }
  }
  if(faqDetail !=null){
  out.println(faqDetail);
  }else{
  out.println("无法获取FAQ详细信息");
  }
   %>
  </body>
</html>
DBUtils
package ajax.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class DBUtils {
private static final String OPTION_FILE_NAME = "ajax_db";

private static String drivers;

private static String url;

private static String user;

private static String password;
static {
/*ResourceBundle res = ResourceBundle.getBundle(OPTION_FILE_NAME);
drivers = res.getString("DRIVERS").trim();
url = res.getString("URL").trim();
user = res.getString("USER").trim();
password = res.getString("PASSWORD").trim();*/
drivers ="com.microsoft.sqlserver.jdbc.SQLServerDriver";
url ="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=aa";
user = "sa";
password = "123456";

}

public static Connection getConnection() throws SQLException {
Connection conn = null;
try {
Class.forName(drivers).newInstance();
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
if (conn == null) {
throw new SQLException("ajax.DBUtils: Cannot get connection.");
}
return conn;
}

public static void close(Connection conn) {
if (conn == null)
return;
try {
conn.close();
} catch (SQLException e) {
System.out.println("ajax.DBUtils: Cannot close connection.");
}
}

public static void close(Statement stmt) {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
System.out.println("ajax.DBUtils: Cannot close statement.");
}

}

public static void close(ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
System.out.println("ajax.DBUtils: Cannot close resultset.");
}
}

}

你可能感兴趣的:(java,sql,Ajax,jsp,Microsoft)