MEMO/Mysql-memo
룰렛 data mysql로 전달
hingu
2024. 6. 13. 16:15
선생님의 폭주 ^^ㅎ
=> 나중에 다시 배울거임! 일단 메모~ 진짜 모르겠땀! ㅎㅅㅎ
https://dev-eunse.tistory.com/143
여기서 룰렛 만들고 왓음ㅎ
🔽 mysql table 생성
create table game(
gidx int(6) not null auto_increment,
mid varchar(30) not null,
mname varchar(30) not null,
ginfo1 varchar(100) not null,
ginfo2 int(4) null, //꽝인경우 null일수 있음
mdate timestamp not null default current_timestamp,
primary key(gidx)
);
🔽 [ MySQL Connector Java ] setting
google => maven repository
https://mvnrepository.com/search?q=mysql
MySQL Connector Java 다운 (8.0.16) -> jar
( 홀수버전 - 베타버전
짝수버전 - 정상버전 )
해당파일 복사 -> WEB-INF > lib에 복사
프로젝트 우클릭 build path > configure build path > add jars > 해당파일 선택 : library 점프시킴
🔽 java파일 생성
- dbconnect.java
( mysql과 java를 연결해주는 파일)- database 접속 아이디 및 패스워드 설정 class
package js; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class dbconnect { public static Connection getConnection() throws ClassNotFoundException, SQLException { String db_driver = "com.mysql.cj.jdbc.Driver"; String db = "jdbc:mysql://localhost:3306/mobile_shop?characterEncoding=UTF-8&serverTimezone=UTC"; // ~3306/해당 database명/ //원래는 database명까지만 쓰면 됨 (time zone error 때문에 이렇게 작성) String user = "coupang"; //mysql -u String pw = "c1004"; //mysql -p Class.forName(db_driver); Connection con = DriverManager.getConnection(db,user,pw); return con; //void 아니라서 } }
timezone error 대응 : https://dev-eunse.tistory.com/145
- gameok.java (servlet)public class gameok extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); String point[][] = { {"500포인트","5000포인트","꽝","3000포인트","2000포인트","1000포인트"}, {"500","5000","1","3000","2000","1000"} }; String result = request.getParameter("result"); String game[] = result.split(","); String mid = request.getParameter("mid"); String mname = request.getParameter("mname"); System.out.println(Arrays.asList(game)); //-- mysql connection try{ //System.out.println("database가 정상 연결되었습니다"); dbconnect dc = new dbconnect(); Connection con = dc.getConnection(); PreparedStatement ps = null; String coupon_name = ""; String point_value = ""; int w=0; while(w<game.length) { //3바퀴(룰렛 돌린만큼 결과값이 담긴 배열) int ww=0; while(ww<point[0].length) { //6바퀴(룰렛 6칸) if(game[w].equals(point[0][ww])) { coupon_name = point[0][ww]; point_value = point[1][ww]; //sql 문법 작성 String sql = "insert into game values('0','"+mid+"','"+mname+"','"+coupon_name+"','"+point_value+"',now())"; ps = con.prepareStatement(sql); ps.executeUpdate(); break; } ww++; } w++; } System.out.println("룰렛게임에 대한 당첨 사항을 point로 지급되었습니다."); ps.close(); con.close(); }catch(Exception e) { System.out.println("database의 정보가 올바르지 않습니다."); } } }
🔽