专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 其他开发语言

jsp网页设计解决办法

发布时间:2011-06-30 07:31:03 文章来源:www.iduyao.cn 采编人员:星星草
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);
%>


<form id="form1" name="form1" method="post" action="">
  <label>
  <select name="user">
<%
try{
while(rs.next())
{
%>
<option value="<%=rs.getString("name")%>"><%=rs.getString("name")%></option>
<%}}catch(Exception e){}
 
 
rs.close();
stmt.close();
con.close();
 
%>
  </select>
  </label>
  </form>
  </td>
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: