Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class Driver {
- public static void main(String[] args) throws SQLException {
- Connection myConn = null;
- PreparedStatement myStmt = null;
- ResultSet myRs = null;
- try {
- // 1. Get a connection to database
- myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "root" , "");
- // 2. Prepare statement
- myStmt = myConn.prepareStatement("select * from employees where salary > ? and department=?");
- // 3. Set the parameters
- myStmt.setDouble(1, 80000);
- myStmt.setString(2, "Legal");
- // 4. Execute SQL query
- myRs = myStmt.executeQuery();
- // 5. Display the result set
- display(myRs);
- //
- // Reuse the prepared statement: salary > 25000, department = HR
- //
- System.out.println("\n\nReuse the prepared statement: salary > 25000, department = HR");
- // 6. Set the parameters
- myStmt.setDouble(1, 25000);
- myStmt.setString(2, "HR");
- // 7. Execute SQL query
- myRs = myStmt.executeQuery();
- // 8. Display the result set
- display(myRs);
- }
- catch (Exception exc) {
- exc.printStackTrace();
- }
- finally {
- if (myRs != null) {
- myRs.close();
- }
- if (myStmt != null) {
- myStmt.close();
- }
- if (myConn != null) {
- myConn.close();
- }
- }
- }
- private static void display(ResultSet myRs) throws SQLException {
- while (myRs.next()) {
- String lastName = myRs.getString("last_name");
- String firstName = myRs.getString("first_name");
- double salary = myRs.getDouble("salary");
- String department = myRs.getString("department");
- System.out.printf("%s, %s, %.2f, %s\n", lastName, firstName, salary, department);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement