-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBCInsere.java
More file actions
36 lines (27 loc) · 1.05 KB
/
JDBCInsere.java
File metadata and controls
36 lines (27 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package connectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;
public class JDBCInsere {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
// conectando
Connection con = new ConnectionFactory().getConnection("localhost", "test", "root", "");
// cria um preparedStatement
String sql = "insert into contatos" +
" (nome,email,endereco,dataNascimento)" +
" values (?,?,?,?)";
PreparedStatement stmt = con.prepareStatement(sql);
// preenche os valores
stmt.setString(1, "Caelum");
stmt.setString(2, "contato@caelum.com.br");
stmt.setString(3, "R. Vergueiro 3185 cj57");
stmt.setDate(4, new java.sql.Date(
Calendar.getInstance().getTimeInMillis()));
// executa
stmt.execute();
stmt.close();
System.out.println("Gravado!");
con.close();
}
}