data-jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="hq.ConnectDB"%>
<%
// 자바파일이 필요하므로 위 코드처럼 임포트합니다.
%>
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");
String type = request.getParameter("type");//로그인 요청인지 회원가입 요청인지를 구분하여 메서드를 실행하도록
//합니다.
//싱글톤 방식으로 자바 클래스를 불러옵니다.
ConnectDB connectDB = ConnectDB.getInstance();
if(type.equals("login")) {
String returns = connectDB.logindb(id, pwd);
out.print(returns);
} else if(type.equals("join")) {
String returns = connectDB.joindb(id, pwd);
out.print(returns);
}
%>
</body>
</html>
ConnectDB.java
package hq;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ConnectDB {
// 싱글톤 패턴으로 사용 하기위 한 코드들
private static ConnectDB instance = new ConnectDB();
public static ConnectDB getInstance() {
return instance;
}
public ConnectDB() {
}
private String jdbcUrl = "jdbc:mysql://내IP:3306/app"; // MySQL 계정 "jdbc:mysql://localhost:3306/DB이름"
private String dbId = "root"; // MySQL 계정 "로컬일 경우 root"
private String dbPw = "alsrb851!!"; // 비밀번호 "mysql 설치 시 설정한 비밀번호"
private Connection conn = null;
private PreparedStatement pstmt = null;
private PreparedStatement pstmt2 = null;
private ResultSet rs = null;
private String sql = "";
private String sql2 = "";
String returns = "";
String returns2 = "";
// 데이터베이스와 통신하기 위한 코드가 들어있는 메서드
public String joindb(String id, String pwd) {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(jdbcUrl, dbId, dbPw);
sql = "select id from user where id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
if (rs.getString("id").equals(id)) { // 이미 아이디가 있는 경우
returns = "id";
}
} else { // 입력한 아이디가 없는 경우
sql2 = "insert into user values(?,?)";
pstmt2 = conn.prepareStatement(sql2);
pstmt2.setString(1, id);
pstmt2.setString(2, pwd);
pstmt2.executeUpdate();
returns = "ok";
}
} catch (Exception e) {
e.printStackTrace();
} finally {if (pstmt != null)try {pstmt.close();} catch (SQLException ex) {}
if (conn != null)try {conn.close();} catch (SQLException ex) {}
if (pstmt2 != null)try {pstmt2.close();} catch (SQLException ex) {}
if (rs != null)try {rs.close();} catch (SQLException ex) {}
}
return returns;
}
public String logindb(String id, String pwd) {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(jdbcUrl, dbId, dbPw);
sql = "select id,pw from user where id=? and pw=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, pwd);
rs = pstmt.executeQuery();
if (rs.next()) {
if (rs.getString("id").equals(id) && rs.getString("pw").equals(pwd)) {
returns2 = "true";// 로그인 가능
} else {
returns2 = "false"; // 로그인 실패
}
} else {
returns2 = "noId"; // 아이디 또는 비밀번호 존재 X
}
} catch (Exception e) {
} finally {if (rs != null)try {rs.close();} catch (SQLException ex) {}
if (pstmt != null)try {pstmt.close();} catch (SQLException ex) {}
if (conn != null)try {conn.close();} catch (SQLException ex) {}
}
return returns2;
}
}
저빨간곳에서 에러가 나는거같은데..
네 안드로이드 데이터를 JSP에서 받으려하는데요.
이렇게 했더니
코드상에 에러는 없는데
Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:963)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3321)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3306)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4021)
at hq.ConnectDB.joindb(ConnectDB.java:45)
at org.apache.jsp.data_jsp._jspService(data_jsp.java:130)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:522)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:1110)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:785)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1425)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
이렇게 오류가 뜹니다.
저게 구글링해보니 파라미터를 한개더받아서 그렇다는데..
어디를 수정해야될지 모르겠습니다.
도와주세요 ㅠㅠ