当前位置: 代码迷 >> 其他开发语言 >> jsp网页设计解决思路
  详细解决方案

jsp网页设计解决思路

热度:1629   发布时间:2013-02-26 00:00:00.0
jsp网页设计
JDBConnection.java代码如下


package com;
import java.sql.*;
public class JDBConnection{

private final String dbDriver=
                        "com.microsoft.jdbc.sqlserver.SQLServerDriver";  //连接sql数据库的方法

private final String url=
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_database02";
private final String userName="sa";
private final String password="";

private Connection con=null;

public JDBConnection(){

  try{
    Class.forName(dbDriver).newInstance();  //加载数据库驱动
  }
  catch(Exception ex){
  }
}



private boolean creatConnection()
{
  try{
  
     con=DriverManager.getConnection(url,userName,psaaword);
 con.setAutoCommit(true);
  }catch(SQLException e){
  }
  return true;
}



public ResultSet executeQuery(String sql)
{
  ResultSet rs;
  try
  {
    if(con==null)
{
 creatConnection();
}

Statement stmt=con.createStatement();

try{

rs=stmt.executeQuery(sql);
}catch(SQLException e){
return null;
}
}catch(SQLException e){
return null;
}
return rs;  
}


public void closeConnection()
{
  if(con!=null)
  {
   try{
   con.close();
   }catch(SQLException e){
   }finally
   {
   con=null;
  }
}
}
}




.jsp文件如下:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@page import="com.JDBConnection"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>

</head>

<body>
<table width="382" height="245" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="36" align="center">将数据库中的内容添加到下拉列表中</td>
  </tr>
  <tr>
  <jsp:useBean id="connection" scope="request" class="com.JDBConnection"/>
    <td height="209" valign="top">

<%
String sql="Seclect *from tb_wyClass";
ResultSet rs=connection.executeQuery(sql);
%>

  相关解决方案