WebServlet.Sqlite OkHttp實現賬戶管理系統的設計

TigerJin發表於2021-09-09

注意:

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

**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;

 

@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);

    }

 

    

    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("").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("").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("").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("").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("").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

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2144/viewspace-2815067/,如需轉載,請註明出處,否則將追究法律責任。

相關文章