如何在不知道列类型的情况下运行JDBC更新查询
我必须使用preparedstatement/statement运行jdbc更新查询,而不知道列类型。 我有一个查询说“更新表集状态=?”?其中id=?' 我得到了一个像{(“status”=“123”),(“id”=“546”)这样的值映射 现在我不知道列类型,有没有通用的方法使用jdbc运行这个查询 而不是运行-ps.setString(1,map.get((“status”); 因为我不知道DB中状态字段的列类型(也可能是int) 请在不使用spring jdbc模板的情况下帮助我解决这个问题。如何在不知道列类型的情况下运行JDBC更新查询,jdbc,Jdbc,我必须使用preparedstatement/statement运行jdbc更新查询,而不知道列类型。 我有一个查询说“更新表集状态=?”?其中id=?' 我得到了一个像{(“status”=“123”),(“id”=“546”)这样的值映射 现在我不知道列类型,有没有通用的方法使用jdbc运行这个查询 而不是运行-ps.setString(1,map.get((“status”); 因为我不知道DB中状态字段的列类型(也可能是int) 请在不使用spring jdbc模板的情况下帮助我解决这个
ps.setString(1,map.get((“status”)
也适用于integer,您只需注意您在integer列中输入的value
是int
类型
以下代码说明:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class SOJDBC
{
public static void main(String rgs[])
{
Connection con = DBConnection.getConnection("TEMP");
try
{
PreparedStatement pstmt = con.prepareStatement("insert into STUDENT(ROLLNO,NAME,AGE) values(?,?,?)");
pstmt.setString(1, "1"); //column type is integer, will work because the value is of int type
pstmt.setString(2, "Bhushan");
pstmt.setString(3, "25"); //column type is integer, will work because the value is of int type
pstmt.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
@user1606084欢迎您,如果这个答案解决了您的问题,您可以接受这个答案。