forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDerbySqlInsertEx.java
More file actions
44 lines (31 loc) · 1.12 KB
/
DerbySqlInsertEx.java
File metadata and controls
44 lines (31 loc) · 1.12 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
37
38
39
40
41
42
43
44
package com.zetcode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DerbySqlInsertEx {
public static void main(String[] args) {
Connection con = null;
PreparedStatement pst = null;
String url = "jdbc:derby://localhost:1527/testdb";
String user = "app";
String password = "app";
String carName = args[0];
int price = Integer.valueOf(args[1]);
try {
con = DriverManager.getConnection(url, user, password);
pst = con.prepareStatement("INSERT INTO CARS(Name, Price) VALUES(?, ?)");
pst.setString(1, carName);
pst.setInt(2, price);
pst.executeUpdate();
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(DerbySqlInsertEx.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
} finally {
DBUtils.closeStatement(pst);
DBUtils.closeConnection(con);
}
}
}