[JAVA] Méthode de connexion DB-JDBC

    1. Chargement de la classe de pilote 2.getConnection 3.

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

public class MConnection { public Connection getConnection() {

// Nom d'utilisateur String user = "●●"; // mot de passe String pass = "●●"; // nom du serveur String servername = "●●";

	Connection conn = null;

	try {

// Charger la classe de pilote JDBC Class.forName("com.mysql.jdbc.Driver");

// Créer une connexion 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 {

// Charger la classe de pilote JDBC Class.forName("com.mysql.jdbc.Driver");

// Créer une connexion conn = getConnection(); // Créer une déclaration 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 = ? ";

// Créer un jeu de résultats pstmt = conn.prepareStatement(sql); pstmt.setString(1,in.getShainId()); rset = pstmt.executeQuery();

// Sortie des données acquises 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 {

// Fermer le traitement 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 {

// Charger la classe de pilote JDBC Class.forName("oracle.jdbc.driver.OracleDriver");

// Créer une connexion conn = getConnection(); conn.setAutoCommit(false); // Créer une déclaration stmt = conn.createStatement(); // SQL String sql = " UPDATE "; sql += " shain_info "; sql += " set "; sql + = "SHAIN_ID =?"; // ID d'employé sql + = ", SHAIN_NM =?"; // Nom de l'employé sql + = ", AGE =?"; // Âge sql + = ", POST_NO =?"; // Code postal sql + = ", ADDRESS_CD =?"; // Code d'adresse sql + = ", ADDRESS2 =?"; // Adresse 2 sql + = ", ADDRESS3 =?"; // Adresse 3 sql + = ", UPDATE_ID = 'system'"; // ID du programme de mise à jour sql + = ", UPDATE_DAY = NOW ()"; // Date de mise à jour sql + = "where"; // Date de mise à jour sql + = "SHAIN_ID =?"; // ID d'employé

// Créer un jeu de résultats 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());

// Exécution de l'instruction INSERT 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 {

// Fermer le traitement 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

Méthode de connexion DB-JDBC
Méthode de connexion JAVA DB
Essayez la connexion DB avec Java
Pool de connexion de base de données Spring Boot
[PDO → Connexion DB avec docker, PHP]
[Java] Connexion avec la base de données locale (IntelliJ + SpringBoot)
Comprendre comment partager des connexions Spring DB (transactions DB)