package mysql.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbConnet {
//driver
static final String MYSQLDRIVER ="com.mysql.jdbc.Driver";
//변하지 않는건 static final 상수로
//URL
static final String MYSQL_URL = "jdbc:mysql://localhost:3306/sist";
//생성자
public DbConnet() {
try {
Class.forName(MYSQLDRIVER); // <-- 어떠한 인자로도 전달하지 않고 그냥 호출만 함
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.err.println("MySql 드라이버 실패(자르파일 없음):"+e.getMessage());
}
}
//계정연결
public Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(MYSQL_URL, "root", "1234");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.err.println("Mysql 연결실패"+e.getMessage());
e.printStackTrace();
}
return conn;
}
//close 메서드 총4개, 오버로딩 메서드
public void dbClose(ResultSet rs, Statement stmt, Connection conn) {
try {
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
public void dbClose(ResultSet rs, PreparedStatement pstmt, Connection conn) {
try {
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
public void dbClose(Statement stmt, Connection conn) {
try {
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
public void dbClose(PreparedStatement pstmt, Connection conn) {
try {
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
}
package mysql.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbConnet {
//driver
static final String MYSQLDRIVER ="com.mysql.jdbc.Driver";
//변하지 않는건 static final 상수로
//URL
static final String MYSQL_URL = "jdbc:mysql://localhost:3306/sist";
//생성자
public DbConnet() {
try {
Class.forName(MYSQLDRIVER); // <-- 어떠한 인자로도 전달하지 않고 그냥 호출만 함
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.err.println("MySql 드라이버 실패(자르파일 없음):"+e.getMessage());
}
}
//계정연결
public Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(MYSQL_URL, "root", "1234");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.err.println("Mysql 연결실패"+e.getMessage());
e.printStackTrace();
}
return conn;
}
//close 메서드 총4개, 오버로딩 메서드
public void dbClose(ResultSet rs, Statement stmt, Connection conn) {
try {
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
public void dbClose(ResultSet rs, PreparedStatement pstmt, Connection conn) {
try {
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
public void dbClose(Statement stmt, Connection conn) {
try {
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
public void dbClose(PreparedStatement pstmt, Connection conn) {
try {
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
}
'DB > mySQL' 카테고리의 다른 글
DB 테이블 2개 만드는법 | varchar(20) | datetime(현재시간 가져옴) (0) | 2022.04.01 |
---|---|
퀀텀DB에서 MYSQL_URL 이랑 MYSQLDRIVER 주소 가져오는법 (0) | 2022.03.31 |
mySQL 이클립스 퀀텀DB 연결하기 (0) | 2022.03.29 |
mySQL cmd(도수창) 에서 create database sist; (데이터베이스 만들고) create table person (테이블만들기) (0) | 2022.03.29 |
mySQL 8 community (이게 조금더 심플한거) 다운받기 (0) | 2022.03.29 |