Code example for DataSource

Methods: getConnection

0
    public void SaveToDB(User user) {
    	String sql = "INSERT INTO users( username, password) VALUES (?,?)";
		Connection conn = null;
		 
		try { 
			conn = dataSource.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, user.getName());
			ps.setString(2, user.getSex());
			ps.executeUpdate();
			ps.close();
  
		} catch (SQLException e) {
			throw new RuntimeException(e);
  
		} finally { 
			if (conn != null) {
				try { 
					conn.close();
				} catch (SQLException e) {}
			}