代码如下:
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);