mariadb-practies 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>com.douzone</groupId>
<artifactId>mariadb-practices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<modules>
<module>jdbc-practices</module>
</modules>
</project>
jdbc-practices 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>
<parent>
<groupId>com.douzone</groupId>
<artifactId>mariadb-practices</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.7.2</version>
</dependency>
</dependencies>
<artifactId>jdbc-practices</artifactId>
</project>
test 패키지 만들고
ConnectionTest.java
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionTest {
public static void main(String[] args) {
Connection conn = null;
try {
// 1. JDBC Driver 로딩
Class.forName("org.mariadb.jdbc.Driver");
// 2. 연결하기
String url = "jdbc:mysql://127.0.0.1:3306/webdb?charset=utf8";
conn = DriverManager.getConnection(url, "webdb", "webdb");
// 3. 연결 성공
System.out.println("ok:" + conn);
} catch (ClassNotFoundException e) {
System.out.println("드라이버 로딩 실패:" + e);
} catch (SQLException e) {
System.out.println("error::" + e);
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
콘솔창에
ok:org.mariadb.jdbc.MariaDbConnection@108c4c35
뜨면 성공
InsertTest01
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class InsertTest01 {
public static void main(String[] args) {
insert("영업");
insert("개발");
insert("기획");
}
public static Boolean insert(String name)
{
Connection conn = null;
java.sql.Statement stmt =null;
boolean result = false;
try {
// 1. JDBC Driver 로딩
Class.forName("org.mariadb.jdbc.Driver");
// 2. 연결하기
String url = "jdbc:mysql://127.0.0.1:3306/employees";
conn = DriverManager.getConnection(url, "hr", "hr");
// 3. Statement
stmt = conn.createStatement();
// 4 .sql문을 실행
String sql ="insert "
+ "into "
+ "dept "
+ "values(null, '"+ name +"')";
int count = stmt.executeUpdate(sql);
result = (count == 1);
} catch (ClassNotFoundException e) {
System.out.println("드라이버 로딩 실패:" + e);
} catch (SQLException e) {
System.out.println("error::" + e);
} finally {
try {
// 자원정리(clean-up)
if (conn != null) {
conn.close();
}
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
// 2.
}
}
DeptVo
package test;
public class DeptVo {
private Long no;
private String name;
public Long getNo() {
return no;
}
public void setNo(Long no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "DeptVo [no=" + no + ", name=" + name + "]";
}
}
UpdataTest01
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class UpdateTest01 {
public static void main(String[] args) {
DeptVo vo = new DeptVo();
vo.setNo(4L);
vo.setName("전략기획팀");
Boolean result = update(vo);
if(result) {
System.out.println("성공");
}
}
public static Boolean update(DeptVo vo) {
Connection conn = null;
java.sql.Statement stmt =null;
boolean result = false;
try {
// 1. JDBC Driver 로딩
Class.forName("org.mariadb.jdbc.Driver");
// 2. 연결하기
String url = "jdbc:mysql://127.0.0.1:3306/employees";
conn = DriverManager.getConnection(url, "hr", "hr");
// 3. Statement
stmt = conn.createStatement();
// 4 .sql문을 실행
String sql ="update dept "
+ "set name='"+ vo.getName() +"' "
+ "where no = '"+vo.getNo()+"'";
int count = stmt.executeUpdate(sql);
result = (count == 1);
} catch (ClassNotFoundException e) {
System.out.println("드라이버 로딩 실패:" + e);
} catch (SQLException e) {
System.out.println("error::" + e);
} finally {
try {
// 자원정리(clean-up)
if (conn != null) {
conn.close();
}
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
// 2.
}
}
DeleteTest01
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DeleteTest01 {
public static void main(String[] args) {
Boolean result = delete(2L);
System.out.println(result ? "성공:" : "실패");
if(result) {
System.out.println("성공");
}
}
public static Boolean delete(Long no)
{
Connection conn = null;
java.sql.Statement stmt =null;
boolean result = false;
try {
// 1. JDBC Driver 로딩
Class.forName("org.mariadb.jdbc.Driver");
// 2. 연결하기
String url = "jdbc:mysql://127.0.0.1:3306/employees";
conn = DriverManager.getConnection(url, "hr", "hr");
// 3. Statement
stmt = conn.createStatement();
// 4 .sql문을 실행
String sql ="delete "
+ "from "
+ "dept "
+ "where no = '"+ no +"' ";
int count = stmt.executeUpdate(sql);
result = (count == 1);
} catch (ClassNotFoundException e) {
System.out.println("드라이버 로딩 실패:" + e);
} catch (SQLException e) {
System.out.println("error::" + e);
} finally {
try {
// 자원정리(clean-up)
if (conn != null) {
conn.close();
}
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
// 2.
}
}
SelectTest01
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SelectTest01 {
public static void main(String[] args) {
search("pat");
}
public static void search(String keyword) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 1. JDBC Driver 로딩
Class.forName("org.mariadb.jdbc.Driver");
// 2. 연결하기
String url = "jdbc:mysql://127.0.0.1:3306/employees?charset=utf8";
conn = DriverManager.getConnection(url, "hr", "hr");
//3. Statement 생성
stmt = conn.createStatement();
//4. SQL 실행
String sql =
"select emp_no, first_name " +
" from employees" +
" where first_name like '%" + keyword + "%'";
rs = stmt.executeQuery(sql);
while(rs.next()) {
Long empNo = rs.getLong(1);
String firstName = rs.getString(2);
System.out.println(empNo + ":" + firstName);
}
} catch (ClassNotFoundException e) {
System.out.println("드라이버 로딩 실패:" + e);
} catch (SQLException e) {
System.out.println("error:" + e);
} finally {
// clean up
try {
if(rs != null) {
rs.close();
}
if(stmt != null) {
stmt.close();
}
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
'Dev > Java' 카테고리의 다른 글
JDBC 처음부터 끝까지 사용해 보기(proj-Bookmall)-2 (0) | 2021.10.07 |
---|---|
JDBC 처음부터 끝까지 사용해 보기(proj-Bookmall)-1 (1) | 2021.10.05 |
JDBC (Programming) (0) | 2021.10.05 |
객체 지향 프로그래밍이 뭘까? (0) | 2021.09.07 |
Java 입문 개발(환경 변수) 설정 tip.이클립스 설정 (0) | 2021.09.06 |