以文本方式查看主题

-  课外天地 李树青  (http://www.njcie.com/bbs/index.asp)
--  JavaEE网站开发课件  (http://www.njcie.com/bbs/list.asp?boardid=20)
----  使用模糊查询的PreparedStatement  (http://www.njcie.com/bbs/dispbbs.asp?boardid=20&id=1049)

--  作者:admin
--  发布时间:2011/6/2 18:45:24
--  使用模糊查询的PreparedStatement

<%@ page c%>
<%@page import="java.sql.*"%>
<html>
<head>
</head>

<body>
<%
        Connection con;
        PreparedStatement pstm;
        ResultSet res;
        ResultSetMetaData rsmd;
        try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                con = DriverManager
                                .getConnection(
                                                "jdbc:sqlserver://localhost:1433; DatabaseName=Students",
                                                "sa", "");
                pstm = con.prepareStatement("select name from stu where name like ?");      //注意之一
                pstm.setString(1,"%德%");                                                                        //注意之二
                res=pstm.executeQuery();
                rsmd = res.getMetaData();
                while (res.next()) {                   
                                out.print(res.getString(1));                   
                }
                con.close();
        } catch (Exception ex) {
                System.out.println(ex.getMessage());
        }
%>
</body>
</html>

 

 

[此贴子已经被作者于2011-06-02 18:45:49编辑过]