使用JDBC将值插入postgresql
问题描述:
我想使用java创建类似任务管理器的东西。我决定使用PostgreSQL来保存我的任务。在这个时刻,我要救我创建的任务到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) {
}
}
我的数据库代码:(带表任务数据库已经被创建带有字段:类型的标题文本,类型文本的描述和类型日期的日期)。这里的数据库类代码:
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.
我试图谷歌这个错误,但没有任何帮助。我应该怎么处理我的约会?
答
您应该在这里使用准备好的语句,这将解决在构建查询时不知道使用什么确切格式的问题。
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注入攻击。这里是一个链接到文件覆盖Postgres的JDBC驱动程序:
+0
它工作得很好!谢谢。是的,问题在声明中。 – Aldres
你应该使用'PreparedStatement'。 – Kayaman