try { Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM user"); } catch (SQLException ex) { ex.printStackTrace(); }
try { PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO user (name, email) VALUES (?, ?)"); preparedStatement.setString(1, "John Doe"); preparedStatement.setString(2, "[email protected]"); preparedStatement.executeUpdate(); } catch (SQLException ex) { ex.printStackTrace(); }In this example, the code prepares a PreparedStatement to insert a new record in the `user` table. If any error occurs while executing the statement, the catch block catches the SQLException and prints the stack trace using the `printStackTrace` method. Package library: java.sql