首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

想把表中的数据显示出来解决办法

2011-12-26 
想把表中的数据显示出来想把表中的数据显示出来.packagegonggaoimportjava.sql.*publicclassGgmain{priv

想把表中的数据显示出来
想把表中的数据显示出来.
package   gonggao;
import   java.sql.*;

public   class   Ggmain   {
private   String   url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydata;user=sa;password=520 ";
public   Ggmain(){
try{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
}
catch   (Exception   ex){
ex.printStackTrace();
}

}


}
---------------------------------------
JSP文件调用BEAN
<%@   page   contentType= "text/html;   charset=gb2312 "   language= "java "   import= "java.sql.* "   errorPage= " "   %>
<jsp:useBean   id= "Ggmain "   class= "gonggao.Ggmain "   scope= "page "> </jsp:useBean>
<html>
<head>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 ">
<title> 无标题文档 </title>
<link   href= "css/textbox.css "   rel= "stylesheet "   type= "text/css ">
</head>

<body>
<%
Connection   con=null;
PreparedStatement   Ggps=null;
ResultSet   Ggrs=null;
String   sqlGg= "select   *   from   Ggaotabelme ";
con=DriverManager.getConnection(url);
Ggps=con.prepareStatement(sqlGg);
Ggrs=Ggps.executeQuery();
if(Ggrs.next()){

%>
<table   width= "100% "   border= "0 "   cellspacing= "0 "   cellpadding= "0 ">
    <tr>
        <td> <p> &nbsp; </p>

        <table   width= "50% "     border= "0 "   align= "center "   cellpadding= "0 "   cellspacing= "0 ">
            <tr>
                <td> <form   name= "form1 "   method= "post "   action= " ">
                    <div   align= "center "> 公告内容 <br>
                        <br>
                        <textarea   name= "textfield "   cols= "60 "   rows= "20 "   class= "textbox "> <%=Ggrs.getString( "G_res ")%> </textarea>
                    </div>
                </form> </td>
                </tr>
        </table>        
        <p   align= "center "> &nbsp; </p> </td>
    </tr>
</table>
<%}
Ggrs.close();
Ggps.close();
con.close();
%>
</body>
</html>
----------------------------
con=DriverManager.getConnection(url);   这一句出现了无法解析url;
是新手,所以不知道怎么搞定,谢谢帮忙.

[解决办法]
首先做数据库链接的操作应该是放在JAVABEAN中来实现,而你却放在JSP里面,也不是不可以,那就失去了JSP+JAVABEAN的意义


我帮你把BEAN改了下,你再运行看看

package gonggao;

import java.sql.*;

public class Ggmain {

public Connection getDataConn(){
final String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydata;user=sa;password=520 ";
Connection conn=null;
try{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
conn=DriverManager.getConnection(url);
}
catch (Exception ex){
ex.printStackTrace();
}
return conn;
}


}
-------------------------------

JSP中的SCRIPTLET也要做一定修改:

<%
Connection conn= Ggmain.getDataConn();
PreparedStatement Ggps=null;
ResultSet Ggrs=null;

String sqlGg= "select * from Ggaotabelme ";

Ggps=conn.prepareStatement(sqlGg);
Ggrs=Ggps.executeQuery();
if(Ggrs.next()){

%>
<table width= "100% " border= "0 " cellspacing= "0 " cellpadding= "0 ">
<tr>
<td> <p> &nbsp; </p>

<table width= "50% " border= "0 " align= "center " cellpadding= "0 " cellspacing= "0 ">
<tr>
<td> <form name= "form1 " method= "post " action= " ">
<div align= "center "> 公告内容 <br/>
<br/>
<textarea name= "textfield " cols= "60 " rows= "20 " class= "textbox "> <%=Ggrs.getString( "TITLE ")%> </textarea>
</div>
</form> </td>
</tr>
</table>
<p align= "center "> &nbsp; </p> </td>
</tr>
</table>
<%}
Ggrs.close();
Ggps.close();
conn.close();
%>

热点排行