Database Connection Hand Out
Database Connection Hand Out
Create Database
public static void main(String[] args) { //The main method is the entry point of
any Java application. The program will start executing from here
Scanner sc = new Scanner(System.in);
// Execute insert
int rowsInserted = pstmt.executeUpdate();// executes the SQL statement
which is INSERT in this case. It will return 1 if the statement is successful
if (rowsInserted > 0) {
System.out.println("Data inserted successfully!");
//This checks if the number of rows affected by the INSERT operation is
greater than 0, indicating that the insert was successful. If successful, it prints "Data
inserted successfully!" to the console.
}
} catch (SQLException e) { //This block catches any SQL exceptions that occur
during the execution of the try block. If any error occurs (like a problem connecting
to the database or an invalid query), the exception is caught here.
e.printStackTrace();
// this prints the details of the exception such as error messages to the
console, helping with debugging
}
}
}