<AndroidTest.java>
package com;
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("/AndroidTest")
public class AndroidTest extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("서버 요청 들어옴!");
String data = request.getParameter("data");
System.out.println("요청된 값 : " + data);
response.setContentType("text/plain; charset=utf-8");
PrintWriter out = response.getWriter();
out.print("{\"result\":\"결과확인\"}");
}
}
<joinController.java>
package com;
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 {
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>
package com;
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 (Exception 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) {
e.printStackTrace();
}
finally {
close();
}
return cnt;
}
}
<MemberVO.java>
package com;
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-JoinActivity>
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 JoinActivity 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_join);
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.33: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(), LoginActivity.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);
}
});
}
}
<Android - LoginActivity>
package com.example.ex_0718;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
}
'안드로이드' 카테고리의 다른 글
영화 박스오피스 App - Volley 라이브러리 활용, 영화진흥위원회 API 활용 (0) | 2022.07.19 |
---|---|
Thread란? (0) | 2022.07.19 |
안드로이드-이클립스 서버 통신 (0) | 2022.07.15 |
Volley (0) | 2022.07.15 |
Adapter View (0) | 2022.07.14 |