본문 바로가기
SpringBoot

자바 데이터 베이스 연동 클래스 설계

by ByteBridge 2014. 11. 7.
반응형

데이터 베이스 연동 클래스


스크린샷 2014-11-07 오전 11.21.32.jpg

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();

}

}


}




반응형