java + mysql 연동
package mysql_connect;
import java.sql.*;
class TableName {
// #################################################################################
int num;
String name;
// #################################################################################
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class db {
// #################################################################################
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; // jdbc 드라이버 주소
static final String DB_URL = "jdbc:mysql://localhost:3306/kjh190106?useSSL=false";
static final String USR_NAME = "root";
static final String USR_PSSR = "1234";
public Connection conn = null;
public Statement stmt = null;
public ResultSet rs = null;
// #################################################################################
// 인스턴스 메서드 (1)
public void dbConnect() {
try {
Class.forName(this.JDBC_DRIVER);
conn = DriverManager.getConnection(this.DB_URL, this.USR_NAME, this.USR_PSSR);
// 접속 결과 출력
if (conn != null) {
System.out.println("접속 성공");
} else {
System.out.println("접속 실패");
}
} catch(ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
}
}
// 인스턴스 메서드 (2)
public void tableSearch() {
String query = "SELECT * FROM movi";
TableName tn = new TableName(); // 객체 생성
try {
this.stmt = this.conn.createStatement();
this.rs = this.stmt.executeQuery(query);
while(this.rs.next()) {
tn.setNum(this.rs.getInt("num"));
tn.setName(this.rs.getString("name"));
System.out.println("no : " + tn.getNum() + " num : " + tn.getName());
}
this.stmt.close();
this.conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
'Java' 카테고리의 다른 글
stu (0) | 2019.03.11 |
---|---|
java + beautifulsoup (0) | 2019.02.09 |
java yaml 파일 읽기 (0) | 2019.01.20 |
java_yaml (0) | 2018.12.16 |