JDBC

JDBC代码

        private static final long serialVersionUID = 1L;
        public static final String DBDRIVER="com.mysql.jdbc.Driver";//org.gjt.mm.mysql.Driver
        public static final String DBURL="jdbc:mysql://localhost:3306/test";
        public static final String DBUSER="root";
        public static final String DBPASS="";       

        request.setCharacterEncoding("GBK");

        response.setContentType("text/html");
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet rSet=null;
        PrintWriter out=response.getWriter();
        String userid=request.getParameter("userid");
        try {
            Class.forName(DBDRIVER);
            connection=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
            String sql="select count(userid) from user where userid=?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1, userid);
            rSet=preparedStatement.executeQuery();
            if(rSet.next()){
                if(rSet.getInt(1)>0)
                    out.print("true");
                else out.print("false");
                out.close();
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

。。。

原文地址:https://www.cnblogs.com/bluewhy/p/4950285.html