import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;public class UpdateDemo extends JFrame { // public class UpdateDemo{
private String dbURL = "jdbc:mysql://localhost/book?useUnicode=true&characterEncoding=GBK"; // 数据库标识名
private String user = "root"; // 数据库用户
private String password = ""; // 数据库用户密码 public UpdateDemo() {
super("添加一本书"); // 调用父类构造函数
final Frame f=new Frame("添加一本书");
Container container = getContentPane(); // 得到容器
final JTextField jtf = new JTextField(18); // 实例化地址输入框
//jtf.setBounds(18, 20, 153, 25);
final JTextField jtf1 = new JTextField(18);
final JTextField jtf2 = new JTextField(18);
final JTextField jtf3 = new JTextField(18);
final JTextField jtf4 = new JTextField(18);
final JTextField jtf5 = new JTextField(18);
final JTextField jtf6 = new JTextField(18);
final JTextField jtf7 = new JTextField(18);
final JButton jb1 = new JButton("确定"); // 实例化按钮
final JButton jb2 = new JButton("浏览...");
JLabel jl = new JLabel("书 名:");
JLabel jl1 = new JLabel("副标题:");
JLabel jl2 = new JLabel("作 者:");
JLabel jl3 = new JLabel("国 籍:");
JLabel jl4 = new JLabel("出版社:");
JLabel jl5 = new JLabel("价 格:");
JLabel jl6 = new JLabel("I S B N :");
JLabel jl7 = new JLabel("封 面:");
container.setLayout(new FlowLayout()); // 设置布局管理器
container.add(jl); // 增加组件到容器上
container.add(jtf);
container.add(jl1);
container.add(jtf1);
container.add(jl2);
container.add(jtf2);
container.add(jl3);
container.add(jtf3);
container.add(jl4);
container.add(jtf4);
container.add(jl5);
container.add(jtf5);
container.add(jl6);
container.add(jtf6);
container.add(jl7);
container.add(jtf7);
container.add(jb2);
// container.add(jtfUrl1);
container.add(jb1);
setSize(300, 768); // 设置窗口尺寸
//距离左上角图标距离100,100
setLocation(0,0);
//设置主键排列方式为靠左
setLayout(new FlowLayout(FlowLayout.LEFT));
//设置主键排列方式为3行2列,水平、垂直间隙为10
//setLayout(new GridLayout(8,2,10,10));
setVisible(true); // 设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗口时退出程序
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ent) {
try {
FileDialog fd=new FileDialog(f,"open file", FileDialog.LOAD);
fd.setVisible(true);//替代fd.show();
String strFile=fd.getDirectory()+fd.getFile();
ImageIcon icon = new ImageIcon(strFile);
JLabel jlb = new JLabel();
scaleImage(icon);
jlb.setIcon(icon); // 将图片加入
String Str7=fd.getDirectory()+fd.getFile();//取得文件完整路径
//System.out.println(Str7);
jtf7.setText(fd.getFile());
//f.add(jlb);
add(jlb);
jlb.setSize(250, 300);
//jlb.setBounds(0,200,100,200);//设置坐标x,y;宽度,高度
jlb.setLocation(0,250);
jlb.setVisible(true);//替代jlb.show();
}catch (Exception ex) {
ex.printStackTrace(); // 输出出错信息
}
}
});
jb1.addActionListener(new ActionListener() { // 按钮事件处理
public void actionPerformed(ActionEvent ent) {
try {
String Str = jtf.getText(); // 得到图像的URL地址
String Str1 = jtf1.getText();
String Str2 = jtf2.getText();
String Str3 = jtf3.getText();
String Str4 = jtf4.getText();
String Str5 = jtf5.getText();
String Str6 = jtf6.getText();
String Str7= jtf7.getText();
System.out.println(Str7);
// System.out.println(Str1);
method(Str,Str1,Str2,Str3,Str4,Str5,Str6,Str7);
} catch (Exception ex) {
ex.printStackTrace(); // 输出出错信息
}
}
});
}
private ImageIcon scaleImage(ImageIcon icon) {
int width = icon.getIconWidth();
//System.out.print(width);
int height = icon.getIconHeight(); if (width <= 80 && height <= 80) {
return icon;
}
Image image = icon.getImage();
image = image.getScaledInstance(80, 80, Image.SCALE_DEFAULT);
return new ImageIcon(image);
}
public void method(String str,String str1,String str2,String str3,String str4,String str5,String str6,String str7){
try {
Class.forName("com.mysql.jdbc.Driver"); // 加载驱动器
Connection con = DriverManager.getConnection(dbURL, user, password); // 获取连接
String sqlStr = "select * from user"; // SQL查询语句
Statement st = con.createStatement(); // 获取PreparedStatement对象
ResultSet rs = st.executeQuery(sqlStr); // 执行查询
String id, title, subhead, authors, nationality, publisher, price, isbn; // 查询结果
// int age;
System.err.println("更新前数据");
while (rs.next()) { // 遍历ResultSet
id = rs.getString("id"); // 获取数据
title = rs.getString("title");
subhead = rs.getString("subhead");
authors = rs.getString("authors");
nationality = rs.getString("nationality");
publisher = rs.getString("publisher");
price = rs.getString("price");
isbn = rs.getString("isbn");
/*
* System.out.println("id: "+id+"\ttitle:"+title+"\tsubhead:"+subhead
* +"\tauthors:"+authors+"\t nationality:"+nationality
* +"\tpublisher:"+publisher+"\tprice"+price+"\t isbn"+isbn);
* //在控制台输出数据
* /*JOptionPane.showMessageDialog(null,"序号: "+id+"\n书名:"
* +title+"\n副标题:"+subhead+"\n作者:"+authors+"\n国籍:"+nationality
* +"\n出版社:"+publisher+"\n价格"+price+"\nISBN"+isbn);
*/
}
// 插入操作
//st.executeUpdate("insert into user values(4, '维基经济学', '大规模协作如何改变一切', '唐·泰普斯科特', '加拿大', '中国青年出版社', '30.00', '9787500676911')");
sqlStr="insert into user(title,subhead,authors,nationality,publisher,price,isbn,url) values('"+str+"','"+str1+"','"+str2+"','"+str3+"','"+str4+"','"+str5+"','"+str6+"','"+str7+"')";
st.executeUpdate(sqlStr);
// 更新操作
// sqlStr="update user set age=21 where id=\'2\'";
// st.executeUpdate(sqlStr); // 删除操作
// sqlStr="delete from user where name=\'Jinnifier\'";
// st.executeUpdate(sqlStr); // 显示修改后结果
System.err.println("更新后数据");
sqlStr = "select * from user"; // SQL查询语句
rs = st.executeQuery(sqlStr); // 执行查询
while (rs.next()) { // 遍历ResultSet
id = rs.getString("id"); // 获取数据
title = rs.getString("title");
subhead = rs.getString("subhead");
authors = rs.getString("authors");
nationality = rs.getString("nationality");
publisher = rs.getString("publisher");
price = rs.getString("price");
isbn = rs.getString("isbn");
/*
* System.out.println("id: "+id+"\ttitle:"+title+"\tsubhead:"+subhead
* +"\tauthors:"+authors+"\t nationality:"+nationality
* +"\tpublisher:"+publisher+"\tprice"+price+"\t isbn"+isbn);
* //在控制台输出数据
* JOptionPane.showMessageDialog(null,"序号: "+id+"\t书名:"
* +title+"\t副标题:"+subhead+"\t作者:"+authors+"\t 国籍:"+nationality
* +"\t出版社:"+publisher+"\t价格"+price+"\t ISBN"+isbn);
*/
}
con.close(); // 关闭连接
} catch (Exception ex) {
ex.printStackTrace(); // 输出出错信息
}
}
public static void main(String[] args) {
new UpdateDemo();
}
}请哪位帮忙改下,我只要实现这个功能,有大段改动也无所谓,呵呵!
"<image width='" + backw+ "'height='" + backh + "'src='"
+ ImageLogin.this.getClass().getResource("loginImage.jpg")
+ "'></img></center></body></html>");希望对你有帮助
private JLabel backLabel;
backLabel = new JLabel();
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;public class UpdateDemo extends JFrame { // public class UpdateDemo{
private String dbURL = "jdbc:mysql://localhost/book?useUnicode=true&characterEncoding=GBK"; // 数据库标识名
private String user = "root"; // 数据库用户
private String password = ""; // 数据库用户密码
JLabel jlb = new JLabel();
JLabel jlbSmall = new JLabel(); public UpdateDemo() {
super("添加一本书"); // 调用父类构造函数
final Frame f=new Frame("添加一本书");
Container contentPane = getContentPane(); // 得到容器
Container container = new Panel();
contentPane.add(container);
contentPane.setLayout(null);
container.setSize(300, 250);
final JTextField jtf = new JTextField(18); // 实例化地址输入框
//jtf.setBounds(18, 20, 153, 25);
final JTextField jtf1 = new JTextField(18);
final JTextField jtf2 = new JTextField(18);
final JTextField jtf3 = new JTextField(18);
final JTextField jtf4 = new JTextField(18);
final JTextField jtf5 = new JTextField(18);
final JTextField jtf6 = new JTextField(18);
final JTextField jtf7 = new JTextField(18);
final JButton jb1 = new JButton("确定"); // 实例化按钮
final JButton browseBtn = new JButton("浏览...");
JLabel jl = new JLabel("书 名:");
JLabel jl1 = new JLabel("副标题:");
JLabel jl2 = new JLabel("作 者:");
JLabel jl3 = new JLabel("国 籍:");
JLabel jl4 = new JLabel("出版社:");
JLabel jl5 = new JLabel("价 格:");
JLabel jl6 = new JLabel("I S B N :");
JLabel jl7 = new JLabel("封 面:");
container.setLayout(new FlowLayout()); // 设置布局管理器
container.add(jl); // 增加组件到容器上
container.add(jtf);
container.add(jl1);
container.add(jtf1);
container.add(jl2);
container.add(jtf2);
container.add(jl3);
container.add(jtf3);
container.add(jl4);
container.add(jtf4);
container.add(jl5);
container.add(jtf5);
container.add(jl6);
container.add(jtf6);
container.add(jl7);
container.add(jtf7);
container.add(browseBtn);
// container.add(jtfUrl1);
container.add(jb1);
contentPane.add(jlb);
contentPane.add(jlbSmall);
setSize(300, 768); // 设置窗口尺寸
//距离左上角图标距离100,100
setLocation(0,0);
//设置主键排列方式为靠左
// setLayout(new FlowLayout(FlowLayout.LEFT));
//设置主键排列方式为3行2列,水平、垂直间隙为10
//setLayout(new GridLayout(8,2,10,10));
setVisible(true); // 设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗口时退出程序
browseBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ent) {
try {
FileDialog fd=new FileDialog(f,"open file", FileDialog.LOAD);
fd.setVisible(true);//替代fd.show();
String strFile=fd.getDirectory()+fd.getFile();
ImageIcon icon = new ImageIcon(strFile);
// scaleImage(icon);
jlb.setIcon(icon); // 将图片加入
String Str7=fd.getDirectory()+fd.getFile();//取得文件完整路径
//System.out.println(Str7);
jtf7.setText(fd.getFile());
//f.add(jlb);
jlb.setSize(250, 300);
//jlb.setBounds(0,200,100,200);//设置坐标x,y;宽度,高度
jlb.setLocation(0,250);
jlb.setVisible(true);//替代jlb.show();
jlb.invalidate();
jlbSmall.setIcon(scaleImage(icon));
jlbSmall.setLocation(0, 550);
jlbSmall.setSize(80, 80);
jlbSmall.setVisible(true);
jlbSmall.invalidate();
}catch (Exception ex) {
ex.printStackTrace(); // 输出出错信息
}
}
});
jb1.addActionListener(new ActionListener() { // 按钮事件处理
public void actionPerformed(ActionEvent ent) {
try {
String Str = jtf.getText(); // 得到图像的URL地址
String Str1 = jtf1.getText();
String Str2 = jtf2.getText();
String Str3 = jtf3.getText();
String Str4 = jtf4.getText();
String Str5 = jtf5.getText();
String Str6 = jtf6.getText();
String Str7= jtf7.getText();
System.out.println(Str7);
// System.out.println(Str1);
method(Str,Str1,Str2,Str3,Str4,Str5,Str6,Str7);
} catch (Exception ex) {
ex.printStackTrace(); // 输出出错信息
}
}
});
}
private ImageIcon scaleImage(ImageIcon icon) {
int width = icon.getIconWidth();
//System.out.print(width);
int height = icon.getIconHeight(); if (width <= 80 && height <= 80) {
return icon;
}
Image image = icon.getImage();
image = image.getScaledInstance(80, 80, Image.SCALE_DEFAULT);
return new ImageIcon(image);
}
public void method(String str,String str1,String str2,String str3,String str4,String str5,String str6,String str7){
try {
Class.forName("com.mysql.jdbc.Driver"); // 加载驱动器
Connection con = DriverManager.getConnection(dbURL, user, password); // 获取连接
String sqlStr = "select * from user"; // SQL查询语句
Statement st = con.createStatement(); // 获取PreparedStatement对象
ResultSet rs = st.executeQuery(sqlStr); // 执行查询
String id, title, subhead, authors, nationality, publisher, price, isbn; // 查询结果
// int age;
System.err.println("更新前数据");
while (rs.next()) { // 遍历ResultSet
id = rs.getString("id"); // 获取数据
title = rs.getString("title");
subhead = rs.getString("subhead");
authors = rs.getString("authors");
nationality = rs.getString("nationality");
publisher = rs.getString("publisher");
price = rs.getString("price");
isbn = rs.getString("isbn");
/*
* System.out.println("id: "+id+"\ttitle:"+title+"\tsubhead:"+subhead
* +"\tauthors:"+authors+"\t nationality:"+nationality
* +"\tpublisher:"+publisher+"\tprice"+price+"\t isbn"+isbn);
* //在控制台输出数据
* /*JOptionPane.showMessageDialog(null,"序号: "+id+"\n书名:"
* +title+"\n副标题:"+subhead+"\n作者:"+authors+"\n国籍:"+nationality
* +"\n出版社:"+publisher+"\n价格"+price+"\nISBN"+isbn);
*/
}
// 插入操作
//st.executeUpdate("insert into user values(4, '维基经济学', '大规模协作如何改变一切', '唐·泰普斯科特', '加拿大', '中国青年出版社', '30.00', '9787500676911')");
sqlStr="insert into user(title,subhead,authors,nationality,publisher,price,isbn,url) values('"+str+"','"+str1+"','"+str2+"','"+str3+"','"+str4+"','"+str5+"','"+str6+"','"+str7+"')";
st.executeUpdate(sqlStr);
// 更新操作
// sqlStr="update user set age=21 where id=\'2\'";
// st.executeUpdate(sqlStr); // 删除操作
// sqlStr="delete from user where name=\'Jinnifier\'";
// st.executeUpdate(sqlStr); // 显示修改后结果
System.err.println("更新后数据");
sqlStr = "select * from user"; // SQL查询语句
rs = st.executeQuery(sqlStr); // 执行查询
while (rs.next()) { // 遍历ResultSet
id = rs.getString("id"); // 获取数据
title = rs.getString("title");
subhead = rs.getString("subhead");
authors = rs.getString("authors");
nationality = rs.getString("nationality");
publisher = rs.getString("publisher");
price = rs.getString("price");
isbn = rs.getString("isbn");
/*
* System.out.println("id: "+id+"\ttitle:"+title+"\tsubhead:"+subhead
* +"\tauthors:"+authors+"\t nationality:"+nationality
* +"\tpublisher:"+publisher+"\tprice"+price+"\t isbn"+isbn);
* //在控制台输出数据
* JOptionPane.showMessageDialog(null,"序号: "+id+"\t书名:"
* +title+"\t副标题:"+subhead+"\t作者:"+authors+"\t 国籍:"+nationality
* +"\t出版社:"+publisher+"\t价格"+price+"\t ISBN"+isbn);
*/
}
con.close(); // 关闭连接
} catch (Exception ex) {
ex.printStackTrace(); // 输出出错信息
}
}
public static void main(String[] args) {
new UpdateDemo();
}
}