JDBC를 사용하여 데이터베이스를 호출하는 일괄 처리 프로그램이 있습니다. 'ID'를 기본 키로하여 일괄 100으로 정보를 전송합니다.Java를 사용하여 여러 SQL 문을 실행하는 방법은 무엇입니까?
데이터베이스를 한 번 호출 한 다음 연결을 닫기 전에 여러 SQL 문을 실행하려고합니다.
나는 참조
내가 루프의 각 ID를 한 번에 세 가지 일을 할 // Database credentials
String USER = username;
String PASS = password;
Connection connec = null;
Statement stmt = null;
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
connec = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = connec.createStatement();
// Step 2: To update the database with the new count and the netppe values.
// Step to know whether the ID is present or not in the database
for(int i=0;i<identities.length;i++){
String booleanString = "SELECT 1 FROM cgm_counters WHERE id = "+identities[i];
stmt.execute(booleanString);
ResultSet resultSet = stmt.getResultSet(); //result set for records
boolean recordFound = resultSet.next();
ResultSet rs = null;
// Retrieve the 'netppe' information.
if(recordFound){
String sql = "SELECT * FROM cgm_counters WHERE id="+identities[i];
rs = stmt.executeQuery(sql);
while(rs.next()){
//Retrieve by column name
double net_ppe = rs.getDouble("spend");
System.out.println("The value of the netppe :"+net_ppe);
}
}// end of 'If' statement
에 대한 코드의 일부를 게시하고있다.
1 > I want to see whether the ID is present or not in the database
2 > If ID - present
{
2 a > retrieve the 'netppe' information for that particular ID
2 b > retrieve the 'count' information for that same ID
2 c> Update the database with the new 'netppe' and 'count' value for the same ID
} else {
Insert the information for the new ID
}
각 ID에 대한 연결을 종료하지 않고 모든 명령문을 실행하는 방법은 무엇입니까? JDBC 및 SQL 입문. 어떤 도움을 주셔서 감사합니다.
은 jdbc 배치를 사용합니다. – Kick
왜 ID마다 연결을 닫아야합니까? 연결을 닫지 않는 코드가 이미 있습니까? – eis
@eis는 'rs.close()'문을 제거했으며 연결을 닫지 않고 여러 문을 실행하는 경우 동일한 단계를 수행합니까? – user3188390