为了账号安全,请及时绑定邮箱和手机立即绑定

WebServlet.Sqlite OkHttp实现账户管理系统的设计

标签:
Android

注意:

===========================================================

**out.println();向用户交互信息

**类型用户的选择type

**增加 删除 修改的Scanner()语句不能加到servelet界面中

**数据库要关闭   防止数据库出现已经锁死的现象   流也要关闭

**OKhttp的使用

===========================================================

 

 

1.建立数据库

[代码]java代码:

?

1

create user(name text,pwd text);

 

2.插入数据(省略)

3.serelet 服务器端

 

login.java

=================

[代码]java代码:

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

package demo01;

 

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.Scanner;

 

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

/**

 * Servlet implementation class   Login

 */

@WebServlet("/login.do")

public class Login extends HttpServlet {

    private static final long serialVersionUID = 1L;

        

  

 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter   out=response.getWriter();

        String   name=request.getParameter("name");

        //String   pwd=request.getParameter("pwd");

        if(name!=null){

            name=new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");

        }

        out.println("欢迎您 !"+name);

        System.out.println(request.getRemoteAddr()+"-"+name);

    }

 

    /**

     * @see   HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

     */

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter   out=response.getWriter();

        request.setCharacterEncoding("UTF-8");

        String    name=request.getParameter("name");

        String   pwd=request.getParameter("pwd");

        String   type=request.getParameter("type");

         

        if(type.equals("login")){

        try {

            String   sql="select * from user where name=? and pwd=?";

            Class.forName("org.sqlite.JDBC");

            Connection   conn=DriverManager.getConnection("jdbc:sqlite:/c:/sqlite/db/my.db");

            PreparedStatement   stmt=conn.prepareStatement(sql);

            stmt.setString(1,name);

            stmt.setString(2,pwd);

            ResultSet   rs=stmt.executeQuery();

            if(rs.next()){

                out.println("登录成功!欢迎"+name);

            }else{

                 

                out.println("用户名或密码错误!");

            }

            rs.close();

            stmt.close();

            conn.close();

            out.close();

        }   catch (Exception   e) {

            e.printStackTrace();

        }

    }else if(type.equals("A")){

        try {

            String   sql="select * from user";

            Class.forName("org.sqlite.JDBC");

            Connection   conn=DriverManager.getConnection("jdbc:sqlite:/c:/sqlite/db/my.db");

            PreparedStatement   stmt=conn.prepareStatement(sql);

            ResultSet   rs=stmt.executeQuery();

            while(rs.next()){

                out.println("姓名:"+rs.getString(1)+"----"+"密码:"+rs.getString(2));

            }

            stmt.close();

            rs.close();

            conn.close();

            out.close();

        }   catch (Exception   e) {

            e.printStackTrace();

        }

         

    }else if(type.equals("B")){

 

         

        String   sql="insert into user values('"+name+"'"+","+"'"+pwd+"'"+")";

         

        try {

            Class.forName("org.sqlite.JDBC");

            Connection   conn=DriverManager.getConnection("jdbc:sqlite:/c:/sqlite/db/my.db");

            Statement   stmt=conn.createStatement();

            int rs=stmt.executeUpdate(sql);

            if(rs>0){

                out.println("添加成功!");

            }else{

                out.println("添加失败!");

            }

            stmt.close();

            conn.close();

            out.close();

        }   catch (Exception   e) {

            e.printStackTrace();

        }

         

    }else if(type.equals("C")){

         String   sql="update user set pwd="+"'"+pwd+"'"+"where   name="+"'"+name+"'";

          try {

            Class.forName("org.sqlite.JDBC");

            Connection   conn=DriverManager.getConnection("jdbc:sqlite:/c:/sqlite/db/my.db");

            Statement   stmt=conn.createStatement();

            int rs=stmt.executeUpdate(sql);

            if(rs>0){

                  out.println("修改成功!");

            }else{

                  out.println("修改失败 !");

            }

            stmt.close();

            conn.close();

        }   catch (Exception   e) {

            e.printStackTrace();

        }

    }else if(type.equals("D")){

        String   sql="delete from User where name="+"'"+name+"'";

        try {

            Class.forName("org.sqlite.JDBC");

            Connection   conn=DriverManager.getConnection("jdbc:sqlite:/c:/sqlite/db/my.db");

            Statement   stmt=conn.createStatement();

            int rs=stmt.executeUpdate(sql);

             

            if(rs>0){

                 out.println("删除成功!");

            }else{

                 out.println("删除失败!");

            }

            stmt.close();

            conn.close();

        }   catch (Exception   e) {

            e.printStackTrace();

        }

         

         

    }

     

         

         

         

    }

 

}

 

..................................................

4.实现测试类

Oklogin.java

[代码]java代码:

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

package demo01;

 

 

import java.io.IOException;

import java.util.Scanner;

 

import okhttp3.FormBody;

import okhttp3.OkHttpClient;

import okhttp3.Request;

import okhttp3.RequestBody;

import okhttp3.Response;

 

public class Oklogin   {

 

    public static void main(String[]   args) {

        boolean flag=false;

        Scanner   sc=new Scanner(System.in);

        System.out.println("欢迎进入学生管理系统的登录页面!");

        System.out.println("请输入用户名:");

        String   name=sc.nextLine();

        System.out.println("请输入密码:");

        String   pwd=sc.nextLine();

        String   type="login";

        OkHttpClient   client=new OkHttpClient();

        RequestBody   body=new FormBody.Builder().add("name",name).add("pwd",pwd).add("type",type).build();

        Request   request=new Request.Builder().url("http://localhost:8080/week/login.do").post(body).build();

        try {

            Response   response=client.newCall(request).execute();

            if(response.isSuccessful()){

                System.out.println(response.body().string());

                flag=true;

            }

        }   catch (Exception   e) {

            e.printStackTrace();

        }

         

        if(flag){

            load();

        }

         

    }

 

    private static void load()   {

         while(true){

                System.out.println("欢迎来到学生账号信息管理系统:");

                System.out.println("--------------------------");

                System.out.println("1:增加用户信息");

                System.out.println("2:删除用户信息");

                System.out.println("3:修改用户信息");

                System.out.println("4:查看用户信息");

                System.out.println("5:退出系统!");

                System.out.println("----------------------------");

                System.out.println("请选择:");

                Scanner   sc=new Scanner(System.in);

                int choose=0;

                while(true){

                    try {

                        choose   = sc.nextInt();

                        break;

                    }   catch (Exception   e) {

                        sc.next();//清除上次输入的信息

                        System.out.println("请输入合法信息:");

                         

                    }

                }

                switch (choose) {

                    case 1:

                        addUser();

                        break;

                    case 2:

                        deleteUser();

                        break;

                    case 3:

                        modifyUser();

                        break;

                    case 4:

                        findAllUser();

                        break;

                    case 5:

                        System.out.println("退出成功!");

                        return;

                }

                 System.out.println("按任意键回到首页:");

                 String   name=sc.next();

                }//while

         

    }

 

    private static void deleteUser()   {

        Scanner   sc=new Scanner(System.in);

        System.out.println("请输入您删除的用户名:");

        String   name=sc.nextLine();

        String   type="D";

        OkHttpClient   client=new OkHttpClient();

        RequestBody   body=new FormBody.Builder().add("name",name).add("type",type).build();

        Request   request=new Request.Builder().url("http://localhost:8080/week/login.do").post(body).build();

        try {

            Response   response=client.newCall(request).execute();

            if(response.isSuccessful()){

                System.out.println(response.body().string());

            }

        }   catch (Exception   e) {

            e.printStackTrace();

        }

         

    }

 

    private static void modifyUser()   {

        Scanner   sc=new Scanner(System.in);

        System.out.println("请输入您修改的用户名:");

        String   name=sc.nextLine();

        System.out.println("请输入您修改的密码:");

        String   pwd=sc.nextLine();

        String   type="C";

        OkHttpClient   client=new OkHttpClient();

        RequestBody   body=new FormBody.Builder().add("name",name).add("pwd",pwd).add("type",type).build();

        Request   request=new Request.Builder().url("http://localhost:8080/week/login.do").post(body).build();

        try {

            Response   response=client.newCall(request).execute();

            if(response.isSuccessful()){

                System.out.println(response.body().string());

            }

        }   catch (Exception   e) {

            e.printStackTrace();

        }    

         

    }

 

    private static void addUser()   {

        Scanner   sc=new Scanner(System.in);

        System.out.println("请输入添加的用户名:");

        String   name=sc.nextLine();

        System.out.println("请输入添加的密码:");

        String   pwd=sc.nextLine();

        String   type="B";

        OkHttpClient   client=new OkHttpClient();

        RequestBody   body=new FormBody.Builder().add("name",name).add("pwd",pwd).add("type",type).build();

        Request   request=new Request.Builder().url("http://localhost:8080/week/login.do").post(body).build();

        try {

            Response   response=client.newCall(request).execute();

            if(response.isSuccessful()){

                System.out.println(response.body().string());

            }

        }   catch (Exception   e) {

            e.printStackTrace();

        }    

    }

    private static void findAllUser()   {

        String   type="A";

        OkHttpClient   client=new OkHttpClient();

        RequestBody   body=new FormBody.Builder().add("type",type).build();

        Request   request=new Request.Builder().url("http://localhost:8080/week/login.do").post(body).build();

        try {

            Response   response=client.newCall(request).execute();

            if(response.isSuccessful()){

                System.out.println(response.body().string());

            }

        }   catch (Exception   e) {

            e.printStackTrace();

        }    

    }

}

 

 

原文链接:http://www.apkbus.com/blog-813041-61027.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消