336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
JAVA 에서
executeQuery 는 SELECT 시 사용됨
INSERT, DELETE 는
executeUpdate 를 사용함
executeQuery 는 resultSet을 생성
executeUpdate 는 resultSet 대신 0과 1로 성공적으로 수행되었는지를 반환
예제 코드
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCEexecuteExample {
public static void main(String[] args) throws SQLException {
Connection connection = null; // connection reference variable for
// getting
// connection
Statement statement = null; // Statement reference variable for query
// Execution
ResultSet resultSet = null; // ResultSet reference variable for saving
// query
// result
String conUrl = "jdbc:mysql://localhost:3306/";
String driverName = "com.mysql.jdbc.Driver";
String databaseName = "student";
String usrName = "root";
String usrPass = "root";
try {
// Loading Driver
Class.forName(driverName);
} catch (ClassNotFoundException e) {
System.out.println(e.toString());
}
try {
// Getting Connection
connection = DriverManager.getConnection(conUrl + databaseName,
usrName, usrPass);
// setting connection autocommit false
connection.setAutoCommit(false);
// Getting reference to connection object
statement = connection.createStatement();
// creating Query String
String updateQuery = "UPDATE student SET NAME='Rajan' WHERE RollNo=1";
String selectQuery = "SELECT * FROM student";
String insertQuery = "INSERT INTO student values(4,'Rohan','MCA','Mumbai')";
String deleteQuery = "DELETE FROM student WHERE RollNo=4";
// Insert Query
statement.executeUpdate(insertQuery);
// Updating Query
int result = statement.executeUpdate(updateQuery);
if (result == 1) {
System.out.println("Table Updated Successfully.......");
}
// Delete Query
statement.executeUpdate(deleteQuery);
// excecuting query
resultSet = statement.executeQuery(selectQuery);
while (resultSet.next()) {
// Didplaying data of tables
System.out.println("Roll No " + resultSet.getInt("RollNo")
+ ", Name " + resultSet.getString("Name") + ", Course "
+ resultSet.getString("Course") + ", Address "
+ resultSet.getString("Address"));
}
} catch (Exception e) {
System.out.println(e.toString());
} finally {
// Closing connection
resultSet.close();
statement.close();
connection.close();
}
}
}