我正在使用itext开发一个项目,当我尝试从数据库中获取数据并使用itext将其填充到已存在的pdf中时,我得到此错误“定位更新不受支持。
package itext.sample;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
/**
* @author prithvi
*
*/
public class FirstPdf {
private static final String Result = "D:/Eclipse Java/image.pdf";
public static void main(String[] args) throws SQLException,IOException,DocumentException {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager
.getConnection("jdbc:mysql://69.167.139.172/bluedb",
"color", "prithvi");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
try {
Statement stm = null;
stm = connection.createStatement();//creating database query
ResultSet rs = null;
rs = stm.executeQuery("SELECT * FROM Sec1");
PdfReader reader = new PdfReader("D:/Eclipse Java/HiltonForms2014_r.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(Result));
AcroFields form = stamper.getAcroFields();
form.setField("LASTNAME", rs.getCursorName());
stamper.setFormFlattening(true);
stamper.close();
reader.close();
connection.close();
}
catch (DocumentException | SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
答案 0 :(得分:1)
您的数据库不支持getCursorName()
,因此会引发数据库访问错误。
查看代码,很难理解为什么需要这种方法。您将获得名为Sec1
的表格的所有字段,并希望获得名称为String
的字段。
假设具有该名称的字段名为lastname
,您可以像这样得到该字段的值:
String lastname = null;
if (rs != null)
lastname = rs.getString("lastname");
您现在可以使用lastname
填写字段:
form.setField("LASTNAME", lastname);
显然只有你知道"lastname"
是否是数据库中调用字段的内容。如果该字段的名称为"last_name"
,"lname"
或任何其他名称,则此示例将无效。