将 blob 插入 sqlite 数据库 java
insert blob into sqlitedatabase java
我已经创建了一个数据库和从用户收集数据的 GUI,现在我想将收集的数据添加到数据库中。与数据库的连接良好,但是单击按钮时调用的函数失败,返回消息 "null pointer exception: null"
系统输出:
Opened database successfully
add to database successfully
java.lang.NullPointerException: null
添加到基础:
private void addtoBase(String name, int row) throws Exception, IOException, SQLException {
PreparedStatement st = null;
FileInputStream fis = null;
Connection con = null;
try {
Class.forName("org.sqlite.JDBC").newInstance();
con = DriverManager.getConnection("jdbc:sqlite:heores.sqlite");
con.setAutoCommit(false);
System.out.println("Opened database successfully");
FileInputStream inputStream= new FileInputStream(selectedFile.getAbsolutePath());
String sql = "INSERT INTO hero (name, row, img) values (?, ?, ?)";
st = con.prepareStatement(sql);
st.setString(1, name);
st.setInt(2, row);
st.setBlob(3,inputStream);
st.executeUpdate();
con.commit();
System.out.println("add to database successfully");
}
catch(Exception e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
finally {
st.close();
fis.close(); //<--- null here?
}
}
按钮动作侦听器:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String name;
int row;
switch(Row.getSelectedItem().toString()){
case "Front":{
row =1;
}
case "Middle":{
row =2;
}
case "Back":{
row =3;
}
default:{
row=0;
}
}
name=name1.getText();
//Here it catches null pointer exeption, however the addtoBase functions seems to work fine all outputs are printed
try {
addtoBase(name, row);
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
}
有什么想法吗?打开数据库后,似乎所有列都已填充,除了 img 列 (Blob),我要插入的文件在 main 中声明:
public class Main extends javax.swing.JFrame {
Image image;
File selectedFile = new File("avatar.png");
//...
我已经检查过
if( input!= null){
function(input);
}
但所有变量和输入似乎都已初始化
更新:好的,我有堆栈跟踪
run:
Opened database successfully
add to database successfully
java.lang.NullPointerException: null
java.lang.NullPointerException
at classes.Main.addtoBase(Main.java:82)
at classes.Main.jButton2ActionPerformed(Main.java:304)
at classes.Main.access0(Main.java:23)
at classes.Main.actionPerformed(Main.java:169)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:697)
at java.awt.EventQueue.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:719)
at java.awt.EventQueue.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
FileInputStream fis = null;
未在代码中的任何位置设置,然后:
finally {
st.close();
fis.close(); //<--- null here?
}
so fis == null 并且您试图关闭它,但出现异常。
你甚至在你的代码中用评论指出了那里。
我已经创建了一个数据库和从用户收集数据的 GUI,现在我想将收集的数据添加到数据库中。与数据库的连接良好,但是单击按钮时调用的函数失败,返回消息 "null pointer exception: null"
系统输出:
Opened database successfully
add to database successfully
java.lang.NullPointerException: null
添加到基础:
private void addtoBase(String name, int row) throws Exception, IOException, SQLException {
PreparedStatement st = null;
FileInputStream fis = null;
Connection con = null;
try {
Class.forName("org.sqlite.JDBC").newInstance();
con = DriverManager.getConnection("jdbc:sqlite:heores.sqlite");
con.setAutoCommit(false);
System.out.println("Opened database successfully");
FileInputStream inputStream= new FileInputStream(selectedFile.getAbsolutePath());
String sql = "INSERT INTO hero (name, row, img) values (?, ?, ?)";
st = con.prepareStatement(sql);
st.setString(1, name);
st.setInt(2, row);
st.setBlob(3,inputStream);
st.executeUpdate();
con.commit();
System.out.println("add to database successfully");
}
catch(Exception e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
finally {
st.close();
fis.close(); //<--- null here?
}
}
按钮动作侦听器:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String name;
int row;
switch(Row.getSelectedItem().toString()){
case "Front":{
row =1;
}
case "Middle":{
row =2;
}
case "Back":{
row =3;
}
default:{
row=0;
}
}
name=name1.getText();
//Here it catches null pointer exeption, however the addtoBase functions seems to work fine all outputs are printed
try {
addtoBase(name, row);
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
}
有什么想法吗?打开数据库后,似乎所有列都已填充,除了 img 列 (Blob),我要插入的文件在 main 中声明:
public class Main extends javax.swing.JFrame {
Image image;
File selectedFile = new File("avatar.png");
//...
我已经检查过
if( input!= null){
function(input);
}
但所有变量和输入似乎都已初始化
更新:好的,我有堆栈跟踪
run:
Opened database successfully
add to database successfully
java.lang.NullPointerException: null
java.lang.NullPointerException
at classes.Main.addtoBase(Main.java:82)
at classes.Main.jButton2ActionPerformed(Main.java:304)
at classes.Main.access0(Main.java:23)
at classes.Main.actionPerformed(Main.java:169)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:697)
at java.awt.EventQueue.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:719)
at java.awt.EventQueue.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
FileInputStream fis = null;
未在代码中的任何位置设置,然后:
finally {
st.close();
fis.close(); //<--- null here?
}
so fis == null 并且您试图关闭它,但出现异常。 你甚至在你的代码中用评论指出了那里。