2017-10-18 5 views
1

私は、javaを使ってタスクマネージャのようなものを作りたいと思っています。 PostgreSQLを使用してタスクを保存することに決めました。JDBCを使ってpostgresqlに値を挿入する

package TaskMgr; 

import java.time.LocalDate; 
import java.time.format.DateTimeFormatter; 
import java.time.format.DateTimeFormatterBuilder; 
import java.util.Locale; 
public class Task { 

private String title; 
private String description; 
private LocalDate dateFormat; 


public Task(String title, String description, String date) { 
    this.title = title; 
    this.description = description; 
    this.dateFormat = setDate(date); 
} 


public LocalDate setDate(String inputDate) { 
    DateTimeFormatter formatter = new DateTimeFormatterBuilder() 
      .parseCaseInsensitive() 
      .appendPattern("yyyy-MM-d") 
      .toFormatter(Locale.ENGLISH); 
    formatter = formatter.withLocale(Locale.ENGLISH); 
    LocalDate outputDate = LocalDate.parse(inputDate, formatter); 
    return outputDate; 

} 

public String getTitle() { 
    return title; 
} 

public String getDescription() { 
    return description; 
} 


public LocalDate getDateFormat() { 
    return dateFormat; 
} 

public static void main(String[] args) { 

} 


} 

とデータベースのための私のコード:この瞬間に、私はこのために、私は次のコードしている、PostgreSQLのに私の作成したタスクを保存する(表のタスクを持つデータベースがすでにフィールドで、作成されています:タイプのタイトルをテキスト、タイプテキストの説明、タイプ日付の日付)。ここDatabaseクラスのコードです:

package TaskMgr; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.time.LocalDate; 
import java.time.Month; 

public class Database { 

private final String url = "jdbc:postgresql://localhost/tasksdb"; 
private final String user = "postgres"; 
private final String password = "31415926"; 

public Connection connect() { 
    Connection conn = null; 
    try { 
     conn = DriverManager.getConnection(url, user, password); 
     System.out.println("Connected to the PostgreSQL server successfully."); 

    } catch (SQLException e) { 
     System.out.println(e.getMessage()); 
    } 

    return conn; 
} 

public void createItem(int idCur, String title, String description, LocalDate date) { 
    try { 
     Statement stmnt = null; 
     stmnt = connect().createStatement(); 
     String sql = "INSERT INTO TASKS (ID,TITLE,DESCRIPTION,DATE) " 
       + "VALUES " + "(" + idCur + ", " + title + ", " + description + ", " + date + ")"; 
     stmnt.executeUpdate(sql); 

    } catch (SQLException e) { 
     System.out.println(e.getMessage()); 
    } 

} 

public static void main(String[] args) { 
    Database database = new Database(); 
    database.connect(); 
    Task task = new Task("'Test2'", "'Test2 description'", "1998-01-07"); 
    database.createItem(2, task.getTitle(), task.getDescription(), task.getDateFormat()); 

} 

}

がしかしのIntelliJは、次のエラーがスローされます。

ERROR: column "date" is of type date but expression is of type integer 
TIP: You will need to rewrite or cast the expression. 

私はこのエラーをGoogleにしようとしましたが、何も助けません。私の日付はどうすればいいですか?

+4

あなたは 'PreparedStatement'を使用する必要があります。ここではPostgresのJDBCドライバをカバーするドキュメントへのリンクです。 – Kayaman

答えて

1

ここで準備文を使用する必要があります。これは、クエリの作成時に使用する正確な形式がわからないという問題を解決します。

PreparedStatement st = conn.prepareStatement("INSERT INTO TASKS (ID, TITLE, DESCRIPTION, DATE) VALUES (?, ?, ?, ?)"); 
st.setInt(1, idCur); 
st.setString(2, title); 
st.setString(3, description); 
st.setObject(4, date); 
st.executeUpdate(); 
st.close(); 

プリペアドステートメントはINSERT文はあなたのPostgresデータベースのための正しい形式で日付とタイムスタンプのリテラルを使用することを確認することの世話をします。

さらに、プリペアドステートメントは、SQLインジェクション攻撃からコードを保護します。

https://jdbc.postgresql.org/documentation/head/

+0

それはうまくいった!ありがとうございました。はい、問題はStatementにありました。 – Aldres

関連する問題