데이터 베이스 연동 클래스
package com.databaseaccess;
/**
* @author Dream
*/
import java.sql.ResultSet;
public interface IDBAccess {
public void openConnection();
public void closeConnection();
public boolean sendQuery(String sql);
public ResultSet selectQuery(String sql);
}
package com.databaseaccess;
/**
* @author Dream
*/
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 DBAccess implements IDBAccess {
Connection connection = null;
Statement statement = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String driverUrl = null;
String driverClassName = null;
String username = null;
String password = null;
@Override
public void openConnection() {
try {
Class.forName(driverClassName);
if (connection == null || connection.isClosed()) {
connection = DriverManager.getConnection(driverUrl, username,
password);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void closeConnection() {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public boolean sendQuery(String sql) {
try {
statement = connection.createStatement();
if (statement.executeUpdate(sql) > 0) {
return true;
} else {
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
@Override
public ResultSet selectQuery(String sql) {
try {
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
return resultSet;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resultSet;
}
}
==================================================
package com.databaseaccess;
/**
* @author Dream
*/
public class MySQL extends DBAccess {
public MySQL(String ip, String port, String sid, String uid, String pwd) {
super();
setConnection(ip, port, sid, uid, pwd);
}
public MySQL(String ip, String port, String sid) {
this(ip, port, sid, “root”, ”****”);
}
public MySQL(String ip) {
this(ip, “3306”, “univmg”, “root”, “****”);
}
public MySQL() {
this(“127.0.0.1”, “3306”, “univmg”, “root”, “****”);
}
private void setConnection(String ip, String port, String sid, String uid,
String pwd) {
driverUrl = “jdbc:mysql://” + ip + ”:” + port + ”/” + sid;
driverClassName = “com.mysql.jdbc.Driver”;
username = uid;
password = pwd;
}
}
============================================
package com.databaseaccess;
/**
* @author Dream
*/
import java.sql.ResultSet;
import java.sql.SQLException;
public class testclient {
public static void main(String[] args) {
try {
IDBAccess idbAccess = new MySQL(“127.0.0.1”, “3306”, “univmg”,”root”,”****”);
idbAccess.openConnection();
String sql = “select * from student”;
ResultSet resultSet = idbAccess.selectQuery(sql);
while (resultSet.next()) {
// Retrieve by column name
int sid = resultSet.getInt(“sid”);
String spassword = resultSet.getString(“spassword”);
String sname = resultSet.getString(“sname”);
int sage = resultSet.getInt(“sage”);
String rgdate = resultSet.getString(“rgdate”);
int subnum = resultSet.getInt(“subnum”);
// Display values
System.out.print(sid);
System.out.print(“\t” + spassword);
System.out.print(“\t” + sname);
System.out.println(“\t” + sage);
System.out.println(“\t” + rgdate);
System.out.println(“\t”+subnum);
}
//int count = resultSet.getInt(1);
idbAccess.closeConnection();
//System.out.println(“데이터 건수 : ” + count);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
'SpringBoot' 카테고리의 다른 글
spring boot 데몬 어플리케이션 만들기 (0) | 2015.09.28 |
---|---|
비트를 이용한 플래그 사용법 (0) | 2015.02.02 |
자바 멤버 관리 클래스 다이어그램 (0) | 2014.10.25 |
자바 비트 스왑 (0) | 2014.10.20 |
자바 (continue label,향상된 for,variable arguments) 사용법 (0) | 2014.10.18 |