- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Java/Swing/Oracle 10g 数据库开发员工管理系统,
有一个名为 photo 的表,其中 photoID
作为数字,photo
作为 blob,我使用 JFileChooser
打开并显示照片,但它不将其保存到我的数据库中。
java.sql.SQLException: Invalid column index
enter code here
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Photo.java
*
* Created on Mar 18, 2014, 5:51:22 PM
*/
package ems;
import java.io.*;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import java.sql.*;
/**
*
* @author css102134
*/
public class Photo extends javax.swing.JFrame {
/** Creates new form Photo */
public Photo() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Jlabel = new javax.swing.JLabel();
browseButton = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Jlabel.setText("choose file:");
browseButton.setText("browse");
browseButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
browseButtonActionPerformed(evt);
}
});
jLabel2.setText("photo");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(Jlabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(browseButton)
.addContainerGap(241, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Jlabel)
.addComponent(browseButton))
.addGap(29, 29, 29)
.addComponent(jLabel2)
.addContainerGap(206, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(this);
File f = fc.getSelectedFile();
String path = f.getAbsolutePath();
Jlabel.setIcon(new ImageIcon(path));
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:XE";
String u="ems2";
String p="ems2";
Connection con = DriverManager.getConnection(url,u,p);
PreparedStatement ps = con.prepareStatement("insert into photo (photos) values(?)");
FileInputStream fin=new FileInputStream("D:/Workspace/ems_ui/photos/prasanth.jpg");
System.out.println("hi");
ps.setBinaryStream(3, fin);
int status = ps.executeUpdate();
if(status>0)
{
jLabel2.setText("successfully inserted in Db");
}
else
{
jLabel2.setText("Image not inserted in Db");
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Photo().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel Jlabel;
private javax.swing.JButton browseButton;
private javax.swing.JLabel jLabel2;
// End of variables declaration
}
最佳答案
有一些事情会跳出来......
您创建一个 PreparedStatement
PreparedStatement ps = con.prepareStatement("insert into photovalues(?,?)");
但仅绑定(bind)单个值 ps.setBinaryStream(3, fin, len);
甚至没有有效的参数...
需要从查询中排除 photoID
参数,或者需要将值绑定(bind)到该参数。
假设 ID 是由数据库自动生成的,那么您可以使用...
PreparedStatement ps = con.prepareStatement("insert into photo (photo) values(?)");
ps.setBinaryStream(1, fin);
如果没有,您需要使用更像...的东西
PreparedStatement ps = con.prepareStatement("insert into photo (photoID, photo) values(?, ?)");
ps.setInt(1, id);
ps.setBinaryStream(2, fin);
此外,您永远不会关闭流,这会使资源保持开放状态并阻止它们被标记为垃圾回收。黄金法则,如果你打开,你就关闭它......
如果您使用的是 Java 7...
try (FileInputStream fin = new FileInputStream(f); Connection con = DriverManager.getConnection(url, u, p)) {...
否则...
FileInputStream fin = null;
Connection con = null;
try {
fin = new FileInputStream(f);
con = DriverManager.getConnection(url, u, p);
//...
} catch (...) {
//..
} finally {
try {
fin.close();
} catch (Excepiton exp) {
}
try {
con.close();
} catch (Excepiton exp) {
}
}
关于java - 如何使用 SQL 浏览图像并将其保存到 Oracle DB 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22497083/
我遇到一个问题,即我的抓取工具正在跳过没有浏览引荐来源网址的浏览页面。我正在尝试解析 URL 中包含/browse/的所有页面,无论引用者如何。 以下是我的代码(根据 paul t 更新): from
我有4个屏幕。 X,A,B,C。导航模式应如下所示 这是代码 X 屏幕 class X extends StatelessWidget { @override Widget build(Bui
我想用 java 编写一个简单的网络浏览器,这是我的代码! import javax.swing.*; import java.io.*; import java.awt.*; import java
这个问题在这里已经有了答案: A home button in iOS 5, xcode 4.2, Story board (2 个回答) 8年前关闭。 来自 FirstView到SecondView
我使用 C#/ASP.Net 在 IIS7 中创建虚拟目录,以便外部人员可以浏览各种文档。 一切看起来都不错,除了浏览是一种沉闷的文本格式。当他们浏览到文件夹时,我如何创建更多的“Windows 资源
我想我在 Chrome 或 Internet Explorer 中见过 Gmail 这样做,但我从未在 Firefox 中见过它。我想我还是会问一下。是否可以在不需要的情况下进行文件上传?我看到你可以
我是 Java 的新手,作为第一次阅读,我阅读了几本有关 Java 语言的书籍。 我有几个关于 Java 文档的问题。如何“导航”它们?是否可以仅使用 Javadoc 来学习新概念? 这是一个示例 -
我有一个解析网络,现在我想浏览标签,或显示图表。我怎样才能得到图表?或者在树中导航。显示第一步然后其他等。并了解这棵树是如何 build 的。 import urllib from lxml impo
考虑以下情况: 杀戮戒指中的 N 项。需要拉取的项目是项目#k 数值论证解决方案不会真正起作用,因为计算或跟踪杀死环中事物的位置很烦人。 最佳答案 实际问题是什么?按 C-y 然后按 M-y k 次有
这是我之前的 question 的后续。 我试图从 here 了解 Haskell 中的列表拆分示例: foldr (\a ~(x,y) -> (a:y,x)) ([],[]) 我可以阅读 Haske
是否有任何 vim 工具通过 Latex 文档结构提供有效的导航。拥有像 NERDTree 面板这样的东西来表示 latex 文档的部分/子部分结构会非常有用。 最佳答案 扩展 mnosefishs
您好,我的应用程序中有一个模态视图 Controller ,当按下某个按钮时,该 Controller 会消失,并且其中带有 UITableView 的 View 会使用导航 Controller 滑
Glances v2.11.1 with psutil v5.4.3 /usr/lib/python3.6/site-packages/psutil/_pslinux.py:1152: Runtime
我正在使用 Eclipse,我希望我可以通过按 STRG 在以驼峰式书写的单词之间跳转。现在我正在使用 Sublime,我找不到这样做的快捷方式,也找不到实现它的插件。 下面的例子说明了我的问题 aF
我的主程序提示用户浏览文件以便使用 ffmpeg 进行转换。这是文件浏览的格式: 1. Select audio file for conversion ( mp3, wma):
我正在尝试浏览 SVN 存储库,而不必检查它: 是否可以在本地(Unix 上)执行此操作? 这可以通过 ssh 访问实现吗? 最佳答案 svn ls 有效。例如 svn ls http://my.sv
我有一个网站,其中列出了数据库中的企业列表。在每个页面上,您可以执行不同的操作,例如将其转发给 friend 、打印页面等。我的问题是我可以使用谷歌分析来跟踪每个列表的展示次数和浏览次数吗?因此,如果
在 Dart 我有一个嵌套的元素列表 void main() { var diction = {'1':'Alpha','2':'Beta','3':{'x':'Football','y':'
当我们的用户单击网页上的浏览按钮来上传任何文件时,我想更改窗口位置。我会给你一个场景 - 我的 html 表单上有一个浏览按钮,当用户单击一个弹出窗口时,该位置默认为“我的文档”。 实际上我想在单击“
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
我是一名优秀的程序员,十分优秀!