Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final String URL = "jdbc:mysql://localhost/exemplows";
- private static final String USER = "XXXXXXXX";
- private static final String SENHA = "XXXXXXX";
- public static Connection obtemConexao() throws SQLException {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block e.printStackTrace();
- }
- return DriverManager.getConnection(URL, USER, SENHA);
- }
- final String sql = "INSERT INTO minha_tabela(coluna_um, coluna_dois) VALUES(?, ?)";
- final Connection conn = obtemConexao();
- final PreparedStatement ps = conn.prepareStatement(sql);
- // seta os valores dos parâmetros
- ps.executeUpdate();
- final String sql = "INSERT INTO minha_tabela(coluna_um, coluna_dois) VALUES (?, ?);";
- final Connection conn = obtemConexao();
- final PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
- // seta os valores dos parâmetros
- ps.executeUpdate();
- final ResultSet rs = ps.getGeneratedKeys();
- if (rs.next()) {
- final int lastId = rs.getInt(1);
- }
- final int lastId = rs.getInt("id");
- ResultSet resultSet = preparedStatement.executeQuery("SELECT LAST_INSERT_ID()");
- if (resultSet.next()) {
- novoId = resultSet.getInt("LAST_INSERT_ID()");
- }
- Connection conexao = null;
- PreparedStatement pst = null;
- ResultSet rs = null;
Add Comment
Please, Sign In to add comment