-
Android 실습 (10)Android 2022. 7. 19. 10:59
< JoinActivity2. java >
package com.example.ex_0718; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.android.volley.AuthFailureError; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import java.util.HashMap; import java.util.Map; public class JoinActivity2 extends AppCompatActivity { EditText txt_id2, txt_nick, txt_pw2; Button btn_add; RequestQueue requestQueue; StringRequest request; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_join2); txt_id2 = findViewById(R.id.txt_id2); txt_nick = findViewById(R.id.txt_nick); txt_pw2 = findViewById(R.id.txt_pw2); btn_add = findViewById(R.id.btn_add); requestQueue = Volley.newRequestQueue(getApplicationContext()); btn_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String id = txt_id2.getText().toString(); String nick = txt_nick.getText().toString(); String pw = txt_pw2.getText().toString(); request = new StringRequest( Request.Method.POST, "http://172.30.1.41:8081/AndroidServer/JoinController", new Response.Listener<String>() { @Override public void onResponse(String response) { // 가입의 성공 여부에 따라 Toast 메시지 띄우기 if (response.equals("1")){ Toast.makeText(getApplicationContext(), "회원가입 성공", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(getApplicationContext(), LoginActivity2.class); startActivity(intent); // 단방향 finish(); }else{ Toast.makeText(getApplicationContext(), "회원가입 실패", Toast.LENGTH_SHORT).show(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } } ){ @Nullable @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<>(); params.put("join_id", id); params.put("join_nick", nick); params.put("join_pw", pw); return params; } }; requestQueue.add(request); } }); } }
< LoginActivity2.java >
package com.example.ex_0718; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class LoginActivity2 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login2); } }
이클립스에서 다음과 같이 생성해 줍니다
< JoinController.java >
import java.io.IOException; import java.io.PrintWriter; 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("/JoinController") public class JoinController extends HttpServlet { private static final long serialVersionUID = 1L; protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("회원가입 요청!"); String id = request.getParameter("join_id"); String pw = request.getParameter("join_pw"); String nick = request.getParameter("join_nick"); System.out.println(id + " / " + pw + " / " + nick); MemberDAO dao = new MemberDAO(); int cnt = dao.join(new MemberVO(id, pw, nick)); PrintWriter out = response.getWriter(); if(cnt == 1) { // 회원가입 성공 out.print("1"); }else { // 회원가입 실패 out.print("0"); } } }
< MemberDAO.java >
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class MemberDAO { Connection conn; PreparedStatement psmt; ResultSet rs; int cnt = 0; public void getCon() { try { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:xe"; String user = "hr"; String pw2 = "hr"; conn = DriverManager.getConnection(url, user, pw2); } catch (Exception e) { e.printStackTrace(); } } public void close() { try { if (rs != null) { rs.close(); } if (psmt != null) { psmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } // 회원가입을 위한 메소드 public int join(MemberVO vo) { getCon(); String sql = "insert into memberinfo values(?, ?, ?)"; try { psmt = conn.prepareStatement(sql); psmt.setString(1, vo.getId()); psmt.setString(2, vo.getPw()); psmt.setString(3, vo.getNick()); cnt = psmt.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { close(); } return cnt; } }
< MemberVO.java >
public class MemberVO { private String id; private String pw; private String nick; public MemberVO(String id, String pw, String nick) { this.id = id; this.pw = pw; this.nick = nick; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPw() { return pw; } public void setPw(String pw) { this.pw = pw; } public String getNick() { return nick; } public void setNick(String nick) { this.nick = nick; } }
< 결과 화면 >
'Android' 카테고리의 다른 글
Android 이론 (4) (0) 2022.07.20 Android 실습 (11) (0) 2022.07.19 Android 실습 (9) (0) 2022.07.18 Android 실습 (8) (0) 2022.07.15 Android 실습 (7) (0) 2022.07.14