为什么结果集为空

时间:2012-12-26 20:01:57

标签: java database jdbc resultset

String name = request.getParameter("type");
String queryString = "SELECT * FROM Customer WHERE TYPE='Provider';";

rs = statement.executeQuery(queryString);
while(rs.next()) out.print(rs.getString(2));

什么都不打印,为什么?我检查了数据库连接,但我已成功连接到它。可能是什么问题?

jsp中的整个代码。我认为整个代码存在问题。这有什么问题?

<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<head> 
        <meta charset="utf-8">

        <title>GET CUSTOMER LIST</title><meta name="keywords" content="contact, direction,"><meta name="description" content="">

        <meta name="HandheldFriendly" content="True">
        <meta name="MobileOptimized" content="320">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">



        <meta name="format-detection" content="telephone=no"> 
        <meta name="format-detection" content="address=no">  

        <link rel="canonical" href="#">

        <link rel="apple-touch-icon-precomposed" sizes="114x114" href="http://www.onbile.com/nbproject/private/sushi/media/icon/apple-touch-icon.png">
        <link rel="apple-touch-icon-precomposed" sizes="72x72" href="http://www.onbile.com/nbproject/private/sushi/media/icon/apple-touch-icon.png">
        <link rel="apple-touch-icon-precomposed" href="http://www.onbile.com/nbproject/private/sushi/media/icon/apple-touch-icon-precomposed.png">
        <link rel="shortcut icon" href="http://www.onbile.com/nbproject/private/sushi/media/icon/apple-touch-icon.png">

        <!-- Mobile IE ows us to activate ClearType technology for smoothing fonts for easy reading -->
        <meta http-equiv="cleartype" content="on">

                <link rel="shortcut icon" href="http://www.onbile.com/favicon.ico">     
        <style type="text/css" media="screen">
            /*reset*/
            html, body { margin: 0; padding: 0; border: 0; }
            body { font-family:Helvetica, Arial, sans-serif; line-height:1.5; font-size:16px; background: #fff; color: #333; word-wrap: break-word; -webkit-text-size-adjust: none; }
            h1, h2, h3, h4, h5, h6{ font-weight: normal; margin: 0;}
            p img { float: left; margin: 0 10px 5px 0; padding: 0; }
            img { border: 0; max-width: 100%; }
            table { width:auto; border-collapse: collapse;border-spacing: 0; }
            article, aside, details, figcaption, figure, footer, header, hgroup, nav, section { display: block; }

            /*Non-semantic helper classes*/
            .ir { display: block; border: 0; text-indent: -999em; overflow: hidden; background-color: transparent; background-repeat: no-repeat; text-align: left; direction: ltr; }
            .ir br { display: none; }

            @font-face {
                font-family: 'FagoCoRegular';
                src: url('http://www.onbile.com/nbproject/private/sushi/media/font/fagoco-webfont.eot');
                src: url('http://www.onbile.com/nbproject/private/sushi/media/font/fagoco-webfont.eot?#iefix') format('embedded-opentype'),
                     url('http://www.onbile.com/nbproject/private/sushi/media/font/fagoco-webfont.woff') format('woff'),
                     url('http://www.onbile.com/nbproject/private/sushi/media/font/fagoco-webfont.ttf') format('truetype'),
                     url('http://www.onbile.com/nbproject/private/sushi/media/font/fagoco-webfont.svg#') format('svg');
                font-weight: normal;
                font-style: normal;
            }
            @font-face {
                font-family: 'FagoCoBlackRegular';
                src: url('http://www.onbile.com/nbproject/private/sushi/media/font/fagoco-black-webfont.eot');
                src: url('http://www.onbile.com/nbproject/private/sushi/media/font/fagoco-black-webfont.eot?#iefix') format('embedded-opentype'),
                     url('http://www.onbile.com/nbproject/private/sushi/media/font/fagoco-black-webfont.woff') format('woff'),
                     url('http://www.onbile.com/nbproject/private/sushi/media/font/fagoco-black-webfont.ttf') format('truetype'),
                     url('http://www.onbile.com/nbproject/private/sushi/media/font/fagoco-black-webfont.svg#FagoCoBlackRegular') format('svg');
                font-weight: normal;
                font-style: normal;
            }

            body {
                font-family: 'FagoCoRegular';
            }
            h2.title1 {
                padding: 22px 15px 20px 15px;
                line-height: 0.9em;
            }
            .title2 {
                line-height: 0.9em;
            }
            .title4 {
                letter-spacing: -0.05em;
                line-height: 1.2em;
            }
            img.image {
                padding: 1px 0;
                display: block;
                width: 100%;
            }
            .imgcontent {
                position: relative;
                overflow: hidden;
                border-top-width: 1px;
                border-top-style: solid;
                border-bottom-width: 1px;
                border-bottom-style: solid;
            }
            img.image.left {
                float: left;
            }
            img.image.right {
                float: right;
            }
            img.image.center {
                margin: 0 auto;
            }
            .order {
                margin-bottom: 1px;
                border-bottom-style: solid;
                border-bottom-width: 1px;
                display: block;
                padding: 4px;
            }
        </style>

        <link rel="stylesheet" href="http://www.onbile.com/assets/websites/final/052/333/052333/css/52333_f.css"><link rel="stylesheet" href="http://www.onbile.com/assets/websites/final/052/333/052333/css/52333_c.css">      
        <!--[if gte IE 8.0]>
        <style type="text/css">
            .t-sushi-p-gallery-content .list-items li {
                width: 46%;
            }
        </style>
        <![endif]-->


        <script src="http://www.onbile.com/nbproject/private/sushi//js/modernizr.custom.09105.js"></script>


    </head> 

    <body>

        <div id="container">



        <header class="t-sushi-p-header int">
            <div class="header">
                <span class="adorn"></span>
                <a href="http://tekirmobile.onbile.com/" class="btn back ir">back</a>
                <h1 class=""><a href="http://tekirmobile.onbile.com/" id="logotype"><img src="http://www.onbile.com/assets/websites/final/052/333/052333/smooth_logo.png" class=" center" style="width:60%;"></a></h1>              <a href="#" class="btn menu ir">menu</a>
                <a href="#container" class="btn menu ir on">menu</a>
            </div>
        </header>




    <header class="t-sushi-p-subtitle">
        <p title="My own profile" class="subtitle edit_element" id="1688529_subtitle" type="text">My own profile</p>
    </header>




        <style>
            #container:target nav.t-sushi-p-menu.acor ul {
                height: 160px;
            }
        </style>
        <nav class="t-sushi-p-menu acor">
            <ul>
                <li class="selected"><a href="http://127.0.0.1/getCustomerList.html" class="menu">GET CUSTOMER LIST</a></li><li class=""><a href="http://tekirmobile.onbile.com//about-me" class="menu">MONITOR CASH FLOW</a></li><li class=""><a href="http://tekirmobile.onbile.com//curriculum-vitae" class="menu">GET CUSTOMER STATE INFO</a></li><li class=""><a href="http://tekirmobile.onbile.com//photo-gallery" class="menu">MONITOR FINANCIAL TRANSACTIONS</a></li><li class=""><a href="http://tekirmobile.onbile.com//blog" class="menu">GET PAYMENT PLAN</a></li>         </ul>
        </nav>











    <div class="t-sushi-p-contact-content">
        <!-- <h2 title="Contact with Mike Stain" class="title1 edit_element" id='1688531_titles' type='text'>Contact with Mike Stain</h2> -->
        <h2 class="title1">GET CUSTOMER LIST</h2>
<form name="input" action="/TekirMobile/getCustomerList" method="post">
        <table>
<div class="prop">



<span class="value "><select name="type" class="middle" size="1">   <option value="All">All</option>
    <option value="Customer" selected="selected">Customer</option>
    <option value="Provider">Provider</option>
    <option value="Bank">Bank</option>
    <option value="Foundation">Foundation</option>
    <option value="Agent">Agent</option>
    <option value="Branch">Branch</option>
    <option value="Contact">Contact</option>
    <option value="Related">Related</option>
    <option value="Personnel">Personnel</option>
</select>
        </span>
    </div>

</table>
<br />
<input type="submit" value="Search" name="submit">


 <% 




try {
/* Create string of connection url within specified format with machine name, 
port number and database name. Here machine name id localhost and 
database name is usermaster. */ 
String connectionURL = "jdbc:mysql://localhost:3306/TekirMobile"; 

// declare a connection by using Connection interface 
Connection connection = null; 

// Load JBBC driver "com.mysql.jdbc.Driver" 
Class.forName("com.mysql.jdbc.Driver").newInstance(); 

/* Create a connection by using getConnection() method that takes parameters of 
string type connection url, user name and password to connect to database. */ 
connection = DriverManager.getConnection(connectionURL, "root", "fenderpass");

 ResultSet rs = null;
 Statement statement = null;


if(request.getParameter("submit")!=null)
 {


String name = request.getParameter("type");
String queryString = "SELECT * FROM Customer WHERE TYPE='Provider'";

out.print(queryString);


rs = statement.executeQuery(queryString);
 for (int i = 1; rs.next(); i++) { 
    out.println("row=" + i + ": " + rs.getString(2));
} 


}

// check weather connection is established or not by isClosed() method 
if(!connection.isClosed())
%>
<font size="+3" color="green"></b>
<% 
connection.close();
}
catch(Exception ex){

%>
</font>
<font size="+3" color="red"></b>
<%
}
%>

</form>








                </div>


    </div>




    <footer class="t-sushi-p-footer">

        <h5 title="" class="title5 edit_element" id="1688532_title_contact" type="text"></h5>
        <div class="small edit_element" id="1688532_contact_description" type="textarea"><p>©2012 | License | Privacy | Özgür Yaz?l?m&nbsp;</p></div>
    </footer>





    <div class="t-sushi-p-social">
        <ul class="social">

            <li class="facebook" id="facebook_elem"> 
                    <a href="http://www.facebook.com/" class="" target="_blank">Facebook</a>
                   </li><li class="twitter" id="twitter_elem"> 
                    <a href="http://www.twitter.com/" class="" target="_blank">Twitter</a>
                   </li><li class="linkedin" style="display:none" id="linkedin_elem"> 
                    <a href="http://www.linkedin.com/" class="" target="_blank">Your Linkedin</a>
                   </li><li class="skype" style="display:none" id="skype_elem"> 
                    <a href="http://www.skype.com/" class="" target="_blank">Your Skype</a>
                   </li><li class="flickr" style="display:none" id="flickr_elem"> 
                    <a href="http://www.flickr.com/" class="" target="_blank">Your Flickr</a>
                   </li><li class="youtube" style="display:none" id="youtube_elem"> 
                    <a href="http://www.youtube.com/" class="" target="_blank">Your Youtube</a>
                   </li><li class="google" style="display:none" id="google_elem"> 
                    <a href="http://www.google.com/" class="" target="_blank">Your Google</a>
                   </li><li class="email" style="display:none" id="email_elem"> 
                    <a href="mailto:" class="" target="_blank"></a>
                   </li>            
        </ul>
    </div>

        </div> <!--! end of #container -->


                        <div style="height:51px;width:100%;clear:both">&nbsp;<br></div>  
                        <div style="position:fixed; bottom:0px;margin-left:auto;margin-right:auto">
                    <!--qzkqc-->
</div>

                        </div>
                    </body>

2 个答案:

答案 0 :(得分:5)

很可能是因为没有返回结果。

答案 1 :(得分:1)

我会尝试一些事情:

在查询结尾处删除分号。传入语句的SQL查询不需要以分号结尾。

将显示的最后一行更改为

for (int i = 1; rs.next(); i++) { 
    out.println("row=" + i + ": " + rs.getString(2));
} 

这样你可以判断根本没有返回任何行,或者返回的所有行都是空字符串。

您的语句永远不会被初始化,尝试使用它将导致NullPointerException。您可能会抛出一个您未看到的异常,因为它会在catch块中没有跟踪的情况下被吃掉。编辑catch块以显示异常中的堆栈跟踪,如下所示:

}
catch(Exception ex){
    ex.printStackTrace();
}