Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class Main {
- private static String getCurrentTimeStamp() {
- Date today = new Date();
- DateFormat dateFormat =new SimpleDateFormat("yy/mm/dd");
- return dateFormat.format(today.getTime()); }
- private static void createDbUserTable() throws SQLException {
- Connection dbConnection = null;
- Statement statement = null;
- String createTableSQL = "CREATE TABLE TEST("
- + "USER_ID NUMBER(5) NOT NULL, "
- + "USERNAME VARCHAR(20) NOT NULL, "
- + "CREATED_BY VARCHAR(20) NOT NULL, "
- + "CREATED_DATE DATE NOT NULL, " + "PRIMARY KEY (USER_ID) "
- + ")";
- String insertTableSQL = "INSERT INTO TEST "
- + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) "
- +"VALUES" +"(1,'mkyong','system', " + "to_date('"
- + getCurrentTimeStamp() + "', 'yy/mm/dd hh24:mi:ss'))";
- try {
- dbConnection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\SMS\\Desktop\\221\\first111.db");
- statement = dbConnection.createStatement();
- statement.execute(createTableSQL);
- System.out.println("Table \"TEST\" is created!");
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- try {
- dbConnection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\SMS\\Desktop\\221\\first111.db");
- statement = dbConnection.createStatement();
- statement.execute(insertTableSQL);
- System.out.println("Table \"TEST\" is Update!");
- }
- catch (SQLException f){
- System.out.println(f.getMessage());
- }
- finally {
- if (statement != null) {
- statement.close();
- }
- if (dbConnection != null) {
- dbConnection.close();
- }
- }
- }
- public static void main(String[] argv) {
- try {
- createDbUserTable();
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement