카테고리 없음
[MySQL] Free MySQL Hosting으로 JSP 파일 연동하기
hyerm_2
2021. 7. 22. 19:19
반응형
SMALL
터미널에 Mysql로 데이터베이스와 테이블을 만들지 않고, 무료로 mysql을 호스팅해주는 사이트가 있다.
https://www.freemysqlhosting.net/
Free MySQL Hosting
Register Create your Free MySQL Database Creating your new database couldn't be easier. Enter your email address above, we'll send you your new password and you're ready to start. Included MySQL Database 5MB MySQL Hosting Space phpMyAdmin Secure, Reliable,
www.freemysqlhosting.net
1. Start my Free Account를 통해 새 계정을 가입한다.
2. 여러 정보 입력 후, 내가 만든 데이타베이스의 이름과 유저네임, 패스워드가 나오게 된다.
3. 위 정보들을 이용하여 이클립스 파일에 사용한다!
dburl = "jdbc:mysql://DATABASE_HOST:3306/DATABASE_NAME
dbUser = "DATABASE_NAME"
dbpasswd = "DATABASE_PASSWORD"
**다음은 예제 코드와 실행한 결과이다.
<index.jsp>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.walab.java.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
roleDAO roledao = new roleDAO();
Role role = roledao.getRole(1);
%>
<h1>id =
<%=role.getRole_id()%>
</h1>
<h3>description =
<%=role.getDescription() %>
</h3>
</body>
</html>
<Role.java>
package com.walab.java;
public class Role {
String description;
int role_id;
public Role(String description, int role_id) {
super();
this.description = description;
this.role_id = role_id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getRole_id() {
return role_id;
}
public void setRole_id(int role_id) {
this.role_id = role_id;
}
}
<RoleDAO.java>
package com.walab.java;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class roleDAO {
private static String dburl = "jdbc:mysql://sql6.freemysqlhosting.net:3306/sql6427042";
private static String dbUser = "sql6427042";
private static String dbpasswd = "8he56Rfa7J";
public Role getRole(Integer role_id) {
Role role = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 1. DBMS에 맞게 Driver를 로드.
Class.forName("com.mysql.jdbc.Driver");
// 2. java SQL 패키지의 DriverMager를 통해서 DBMS에 연결
conn = DriverManager.getConnection(dburl, dbUser, dbpasswd);
// 3.Sql 쿼리 String 생성
String query = "SELECT description,role_id FROM role WHERE role_id = ?";
// 4.DB에 Sql 쿼리 전달 하기 위한 객체 생성
ps = conn.prepareStatement(query);
// 3에서 ?에 대한 설정 , 1번째 ?로 에 값을 설정
ps.setInt(1, role_id);
// 5. 쿼리 실행 및 결과 얻기
rs = ps.executeQuery();
// 결과 값 빼내기 nex()함수의 결과 첫번재 컬럼을 빼난다.
if (rs.next()) {
// index로 뽄는 방법
String description = rs.getString(1);
// Column명으로 뽑
int id = rs.getInt("role_id");
role = new Role(description, id);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return role;
}
}
<pom.xml>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>JSP_lab</groupId>
<artifactId>JSP_lab</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>16</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
</dependencies>
</project>
# Package Explorer
# 결과
반응형
LIST