[JAVA] DB connection method-JDBC

    1. Loading driver class 2.getConnection 3.

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException;

public class MConnection { public Connection getConnection() {

// User name String user = "●●"; // password String pass = "●●"; // server name String servername = "●●";

	Connection conn = null;

	try {

// Load JDBC driver class Class.forName("com.mysql.jdbc.Driver");

// Create Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080/" + servername , user, pass); return conn;

	} catch (ClassNotFoundException e) {
		return null;
	} catch (SQLException e) {
		return null;
	} catch (Throwable e) {
		return null;
	}
}

}

import java.math.BigDecimal; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;

import com.kaishamei.data.DShainData;

public class MShainInfo extends MConnection{ /** * @param in * @return */ public DShainData getShainInfo(DShainData in) {

	Connection conn = null;
	Statement stmt = null;
	ResultSet rset = null;
	PreparedStatement pstmt = null;
	DShainData info = null;

	try {

// Load JDBC driver class Class.forName("com.mysql.jdbc.Driver");

// Create Connection conn = getConnection(); // Create Statement stmt = conn.createStatement(); // SQL String sql = "select "; sql += " SHAIN_ID "; sql += " ,SHAIN_NM "; sql += " ,AGE "; sql += " ,POST_NO "; sql += " ,ADDRESS_CD "; sql += " ,ADDRESS2 "; sql += " ,ADDRESS3 "; sql += " ,PASSWORD "; sql += " ,DEL_KBN "; sql += " ,ENTRY_ID "; sql += " ,ENTRY_DAY "; sql += " ,UPDATE_ID "; sql += " ,UPDATE_DAY "; sql += " from SHAIN_INFO "; sql += " where "; sql += " SHAIN_ID = ? ";

// Create Resultset pstmt = conn.prepareStatement(sql); pstmt.setString(1,in.getShainId()); rset = pstmt.executeQuery();

// Output the acquired data while (rset.next()) { info = new DShainData();

			info.setShainId(rset.getString("SHAIN_ID"));
			info.setShainNm(rset.getString("SHAIN_NM"));
			BigDecimal age = rset.getBigDecimal("AGE");
			int iAge = 0;
			if (age != null) {
				iAge = Integer.valueOf(age.toString());
			}
			info.setAge(iAge);
			info.setPostNo(rset.getString("POST_NO"));
			info.setAddressCd(rset.getString("ADDRESS_CD"));
			info.setAddress2(rset.getString("ADDRESS2"));
			info.setAddress3(rset.getString("ADDRESS3"));
			info.setPassword(rset.getString("PASSWORD"));
			info.setDelKbn(rset.getString("DEL_KBN"));
			info.setEntryId(rset.getString("ENTRY_ID"));
			info.setEntryDay(rset.getString("ENTRY_DAY"));
			info.setUpdateId(rset.getString("UPDATE_ID"));
			info.setUpdateDay(rset.getString("UPDATE_DAY"));
			break;
		}

	} catch (SQLException e) {
		String msg = e.toString();
		System.out.println(msg);
	} catch (Throwable e) {
		String msg = e.toString();
		System.out.println(msg);
	} finally {
		try {

// Close processing if (rset != null) { rset.close(); rset = null; }

			if (stmt != null) {
				stmt.close();
				stmt = null;
			}

			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (Throwable e) {
			// nop
			String msg = e.toString();
			System.out.println(msg);
		}
	}
	return info;
}

public void upDateSInfo(DShainData upData) {

	Connection conn = null;
	Statement stmt = null;
	PreparedStatement pstmt = null;

	try {

// Load JDBC driver class Class.forName("oracle.jdbc.driver.OracleDriver");

// Create Connection conn = getConnection(); conn.setAutoCommit(false); // Create Statement stmt = conn.createStatement(); // SQL String sql = " UPDATE "; sql += " shain_info "; sql += " set "; sql + = "SHAIN_ID =?"; // Employee ID sql + = ", SHAIN_NM =?"; // Employee name sql + = ", AGE =?"; // Age sql + = ", POST_NO =?"; // Zip code sql + = ", ADDRESS_CD =?"; // Address code sql + = ", ADDRESS2 =?"; // Address 2 sql + = ", ADDRESS3 =?"; // Address 3 sql + = ", UPDATE_ID ='system'"; // Updater ID sql + = ", UPDATE_DAY = NOW ()"; // Update date sql + = "where"; // Update date sql + = "SHAIN_ID =?"; // Employee ID

// Create Resultset pstmt = conn.prepareStatement(sql); pstmt.setString(1,upData.getShainId()); pstmt.setString(2,upData.getShainNm()); String sAge = Integer.toString(upData.getAge()); pstmt.setString(3,sAge); pstmt.setString(4,upData.getPostNo()); pstmt.setString(5,upData.getAddressCd()); pstmt.setString(6,upData.getAddress2()); pstmt.setString(7,upData.getAddress3()); pstmt.setString(8,upData.getShainId());

// INSERT statement execution pstmt.execute(); // COMMIT conn.commit();

	} catch (ClassNotFoundException e) {
		String msg = e.toString();
		System.out.println(msg);
	} catch (SQLException e) {
		try {
			conn.rollback();
		} catch (SQLException e1) {
		}
		String msg = e.toString();
		System.out.println(msg);
	} catch (Throwable e) {
		String msg = e.toString();
		System.out.println(msg);
		throw e;
	} finally {
		try {

// Close processing if (stmt != null) { stmt.close(); stmt = null; }

			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (Throwable e) {
			// nop
			String msg = e.toString();
			System.out.println(msg);
		}
	}

}

}

Recommended Posts

DB connection method-JDBC
JAVA DB connection method
Try DB connection with Java
Spring Boot DB connection pool
[PDO → DB connection with docker, PHP]
[Java] Connection with local DB (IntelliJ + SpringBoot)
Understand how to share Spring DB connection (DB transaction)