组合框的内容添加到数据库的问题
本帖最后由 yugijiudai 于 2013-01-23 00:54:22 编辑 代码如下:
import java.io.*;//导入IO操作所用包
import java.sql.*;//导入sql操作包
public class AddmagicManage {
public static int add(String co,String name,String ct,String cl,String ci,File file)
{
PreparedStatement pstmt = null;//预编译对象
int rows=0;//更新语句行数变量
try
{
Connection conn=DBConnection.getConnection();
String sql="insert into 魔法表(卡号,卡片名称,卡片种类,卡片限制,效果说明,卡图) values(?,?,?,?,?,?)";
pstmt=conn.prepareStatement(sql);//使用预编译语句进行插入操作
FileInputStream fis=new FileInputStream(file);//定义字节输入流
pstmt.setString(1,co);
pstmt.setString(2,name);
pstmt.setString(3,ct);
pstmt.setString(4,cl);
pstmt.setString(5,ci);
pstmt.setBinaryStream(6, fis, (int)file.length());//将字节流设置到SQL中*/
rows=pstmt.executeUpdate(sql);//执行更新操作
fis.close();
pstmt.close();//关闭SQL操作
conn.close();//关闭数据库连接
}
catch (SQLException e) //判断是否发生异常
{
System.out.println("插入二进制类型数据时发生异常");
e.printStackTrace();
}
catch(IOException e)//判断是否发生IO处理异常
{
System.out.println("IO操作发生异常");
}
catch (Exception e) //判断是否发生异常
{
System.out.println("数据库操作过程发生异常");
}
return rows;
}
}
import java.io.File;//导入文件类
import java.awt.*;//导入awt包
import java.awt.event.*;//导入事件包
import javax.swing.*;//导入swing控件包
public class addmagicSwing extends JFrame implements ActionListener
{
JLabel Cardno = new JLabel("卡号:");
JLabel Cardname = new JLabel("卡片名称:");
JLabel Cardtype = new JLabel("卡片种类:");
JLabel Cardlimit = new JLabel("卡片限制:");
JLabel Cardinfo = new JLabel("效果说明:");
JLabel result = new JLabel("显示信息");
JButton Addpic = new JButton("添加卡图");
JButton Addcard = new JButton("添加此卡片信息");
JButton reback = new JButton("返回");
JTextField textField = new JTextField();
JTextField textField_4 = new JTextField();
JTextArea textArea = new JTextArea();
JComboBox comboBox = new JComboBox();
JComboBox comboBox_1 = new JComboBox();
JLabel tupian = new JLabel(" ");
DBConnection conn=new DBConnection();
public addmagicSwing() {
setTitle("添加卡片信息界面");
getContentPane().setLayout(null);
Cardno.setFont(new Font("宋体", Font.PLAIN, 15));
Cardno.setBounds(50, 50, 38, 18);
getContentPane().add(Cardno);
Cardname.setFont(new Font("宋体", Font.PLAIN, 15));
Cardname.setBounds(50, 90, 68, 18);
getContentPane().add(Cardname);
Cardtype.setFont(new Font("宋体", Font.PLAIN, 15));
Cardtype.setBounds(50, 130, 68, 18);
getContentPane().add(Cardtype);
Cardlimit.setFont(new Font("宋体", Font.PLAIN, 15));
Cardlimit.setBounds(50, 209, 68, 18);
getContentPane().add(Cardlimit);
Cardinfo.setFont(new Font("宋体", Font.PLAIN, 15));
Cardinfo.setBounds(50, 367, 68, 18);
getContentPane().add(Cardinfo);
Addpic.setBounds(515, 50, 89, 23);
getContentPane().add(Addpic);
Addpic.addActionListener(this);
Addcard.setBounds(751, 480, 117, 23);
getContentPane().add(Addcard);
Addcard.addActionListener(this);
textField.setBounds(218, 90, 161, 20);
getContentPane().add(textField);
textField.setColumns(10);
textArea.setBounds(211, 324, 431, 209);
getContentPane().add(textArea);
comboBox.setFont(new Font("宋体", Font.PLAIN, 15));
comboBox.setModel(new DefaultComboBoxModel(new String[] {"全部","普通魔法", "永续魔法", "速攻魔法", "仪式魔法","场地魔法","装备魔法"}));
comboBox.setBounds(218, 116, 104, 44);
getContentPane().add(comboBox);
textField_4.setBounds(218, 48, 161, 20);
getContentPane().add(textField_4);
textField_4.setColumns(10);
comboBox_1.setFont(new Font("宋体", Font.PLAIN, 15));
comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"无限制", "准限制", "限制", "禁止"}));
comboBox_1.setBounds(218, 183, 89, 44);
getContentPane().add(comboBox_1);
tupian.setBounds(642, 90, 177, 254);
getContentPane().add(tupian);
reback.setBounds(755, 577, 93, 23);
getContentPane().add(reback);
reback.addActionListener(this);
result.setFont(new Font("宋体", Font.PLAIN, 15));
result.setBounds(775, 529, 93, 23);
getContentPane().add(result);
this.setVisible(true);
this.setBounds(500, 100, 1000, 800);
}
public void actionPerformed(ActionEvent e)//事件方法
{
String co=textField_4.getText(); //卡号
String name=textField.getText(); //卡名
String ct=comboBox.getSelectedItem().toString(); //卡种类
String cl=comboBox_1.getSelectedItem().toString(); //卡限制
String ci=textArea.getText(); //效果说明
File file=null; //卡图
if(e.getSource()==Addpic)//如果添加照片触发事件
{
JFileChooser jfc=new JFileChooser();
jfc.showSaveDialog(this);//弹出文件选择框
file=jfc.getSelectedFile();//获取选择的照片文件
tupian.setIcon(new ImageIcon(file.getAbsolutePath()));//将照片显示在标签中
}
if(e.getSource()==reback)
{
this.dispose();
new AdminSwing();
}
if(e.getSource()==Addcard)//如果添加学生按钮触发事件
{
if(AddmagicManage.add(co,name,ct,cl,ci,file)>0)//判断是否更新了数据
{
result.setText("添加成功");//设置结果标签内容
}
else
{
result.setText("添加失败");//设置结果标签内容
}
}
}
}
功能如下:按添加此卡片信息的按钮会根据组合框和文本区的内容添加到数据库的“魔法表”里,但每次添加都说失败,数据库里"魔法表"的卡号,卡片名称,卡片种类,卡片限制,效果说明都是varchar类型,卡图是image类型,不知道哪里出了问题,求各位大大解释一下,谢谢了
[解决办法]
个人来说,用数据库只保存图片路径相对简单。
你上传图片的时候,会有一个图片文件,然后把你这个图片保存到硬盘某文件夹里面,再把这个路径提取出来,保存到数据库,当需要加载图片的时候,只需要根据路径去加载图片即可。
而把图片保存到硬盘,只需要使用FileUtils.copyFile(srcFile, destFile),其中,destFile为你要保存图片的全路径,包括图片名,srcFile是你上传的文件,就可以做到,很方便。
用法为:
String filePath="xxxxxx/aa.jsp";
File destFile = new File("xxxxxx/aa.jsp");
File srcFile //你上传的图片文件。
FileUtils.copyFile(srcFile, destFile)。
然后把filePath保存到你的数据库就行了。
通常,为了上传的文件不重复,会设计一个表来保存图片的信息,比如图片名,类型,图片存放的路径,还有图片别名。这个图片别名就是为了不与其它图片重名设计的,可以不带拓展名,在使用的时候,把拓展名加上就行了。
比如我会把上传的图片,别名取当前时间的yyyymmddhhmissSSSS格式。这个具体要看需求是什么了,怎么方便怎么设计。