数据库如下
create table book(
number varchar(10),
bookname varchar(10),
publish varchar(10),
author varchar(10),
isbn varchar(10),
price varchar(10)
)
代码:import java.io.*;
import java.net.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.*;
import java.awt.event.*;import javax.swing.*;
public class UpdateBook extends JPanel{
private JTextField number,bookname,publish,author,isbn,price;
private JLabel jnumber,jbookname,jpublish,jauthor,jisbn,jprice;
private JButton button1,button2;
PrintStream output;
DataInputStream input;
String message="";
String bookinfor[];
//构造方法
public UpdateBook(){
this.setLayout(null);
jnumber=new JLabel("编号");
number=new JTextField();
jbookname=new JLabel("书名");
bookname=new JTextField();
jpublish=new JLabel("出版社");
publish=new JTextField();
jauthor=new JLabel("作者");
author=new JTextField();
jisbn=new JLabel("ISBN");
isbn=new JTextField();
jprice=new JLabel("价格");
price=new JTextField();
button1=new JButton("查询");
button2=new JButton("修改");
//设置控件的位置和大小
jnumber.setBounds(30,20,60,26);
jbookname.setBounds(30,100,60,26);
jpublish.setBounds(30,130,60,26);
jauthor.setBounds(30,160,60,26);
jisbn.setBounds(30,190,60,26);
jprice.setBounds(30,220,60,26);
number.setBounds(110,20,120,26);
bookname.setBounds(110,100,120,26);
publish.setBounds(110,130,120,26);
author.setBounds(110,170,120,26);
isbn.setBounds(110,190,120,26);
price.setBounds(110,220,120,26);
button1.setBounds(60,60,60,30);
button2.setBounds(60,250,60,26);
//设置文本框的属性为不可编辑
bookname.setEnabled(false);
publish.setEnabled(false);
author.setEnabled(false);
isbn.setEnabled(false);
price.setEnabled(false);
//将控件添加到容器中
this.add(jnumber);
this.add(number);
this.add(button1);
this.add(jbookname);
this.add(bookname);
this.add(jpublish);
this.add(publish);
this.add(jauthor);
this.add(author);
this.add(jisbn);
this.add(isbn);
this.add(jprice);
this.add(price);
this.add(button2);
//为button1增加监听
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
if(number.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "编号不能为空","修改",JOptionPane.WARNING_MESSAGE);
String sqlstr1;
JdbcFiles conn=new JdbcFiles();
sqlstr1="select number,bookname,publish,author,isbn,price from book where convert(nvarchar(255),number)='"+number.getText()+"'";
ResultSet result=conn.executeQuery(sqlstr1);
if(result.next()){
{
bookname.setText(result.getString("password"));
publish.setText(result.getString("college"));
author.setText(result.getString("subject"));
isbn.setText(result.getString("grade"));
price.setText(result.getString("name"));
bookname.setEnabled(true);
publish.setEnabled(true);
author.setEnabled(true);
isbn.setEnabled(true);
price.setEnabled(true);
}
}
else
JOptionPane.showMessageDialog(null, "当前没有该书信息","修改",JOptionPane.WARNING_MESSAGE);
}catch (ClassNotFoundException ce) {
System.out.println("SQLException:" + ce.getMessage()); } catch (SQLException ex) {
System.out.println(ex); } catch (Exception s) {
s.printStackTrace();
} }
});
//为button2增加监听
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
String sqlstr2="update book set bookname='"+bookname.getText()+"',publish='"+publish.getText()+"',author='"+author.getText()+"',isbn='"+isbn.getText()+"',price='"+price.getText()+"' where convert(nvarchar(255),number)='"+number.getText()+"'";
int k=-1;
JdbcFiles conn1=new JdbcFiles();
k=conn1.insert(sqlstr2);
if(number.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "编号不能为空","修改",JOptionPane.WARNING_MESSAGE);
else
if(bookname.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "书名不能为空","修改",JOptionPane.WARNING_MESSAGE);
else
if(publish.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "出版社不能为空","修改",JOptionPane.WARNING_MESSAGE);
else
if(author.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "作者不能为空","修改",JOptionPane.WARNING_MESSAGE);
else
if(isbn.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "ISBN不能为空","修改",JOptionPane.WARNING_MESSAGE);
else
if(price.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "价格不能为空","修改",JOptionPane.WARNING_MESSAGE);
else if (k > -1)
{
JOptionPane.showMessageDialog(null, "修改成功!", "修改",JOptionPane.WARNING_MESSAGE);
conn1.close();
} else
JOptionPane.showMessageDialog(null, "修改失败!", "修改",JOptionPane.WARNING_MESSAGE);
}catch(ClassNotFoundException ce){
System.out.println("SQLException:"+ce.getMessage());
}catch(SQLException ex){
System.out.println(ex);
}catch(Exception s){
s.printStackTrace();
}
}
});
setSize(250,380);
this.setVisible(true);
}
public static void main(String[] args)
{
UpdateBook app1=new UpdateBook();
}
}
create table book(
number varchar(10),
bookname varchar(10),
publish varchar(10),
author varchar(10),
isbn varchar(10),
price varchar(10)
)
代码:import java.io.*;
import java.net.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.*;
import java.awt.event.*;import javax.swing.*;
public class UpdateBook extends JPanel{
private JTextField number,bookname,publish,author,isbn,price;
private JLabel jnumber,jbookname,jpublish,jauthor,jisbn,jprice;
private JButton button1,button2;
PrintStream output;
DataInputStream input;
String message="";
String bookinfor[];
//构造方法
public UpdateBook(){
this.setLayout(null);
jnumber=new JLabel("编号");
number=new JTextField();
jbookname=new JLabel("书名");
bookname=new JTextField();
jpublish=new JLabel("出版社");
publish=new JTextField();
jauthor=new JLabel("作者");
author=new JTextField();
jisbn=new JLabel("ISBN");
isbn=new JTextField();
jprice=new JLabel("价格");
price=new JTextField();
button1=new JButton("查询");
button2=new JButton("修改");
//设置控件的位置和大小
jnumber.setBounds(30,20,60,26);
jbookname.setBounds(30,100,60,26);
jpublish.setBounds(30,130,60,26);
jauthor.setBounds(30,160,60,26);
jisbn.setBounds(30,190,60,26);
jprice.setBounds(30,220,60,26);
number.setBounds(110,20,120,26);
bookname.setBounds(110,100,120,26);
publish.setBounds(110,130,120,26);
author.setBounds(110,170,120,26);
isbn.setBounds(110,190,120,26);
price.setBounds(110,220,120,26);
button1.setBounds(60,60,60,30);
button2.setBounds(60,250,60,26);
//设置文本框的属性为不可编辑
bookname.setEnabled(false);
publish.setEnabled(false);
author.setEnabled(false);
isbn.setEnabled(false);
price.setEnabled(false);
//将控件添加到容器中
this.add(jnumber);
this.add(number);
this.add(button1);
this.add(jbookname);
this.add(bookname);
this.add(jpublish);
this.add(publish);
this.add(jauthor);
this.add(author);
this.add(jisbn);
this.add(isbn);
this.add(jprice);
this.add(price);
this.add(button2);
//为button1增加监听
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
if(number.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "编号不能为空","修改",JOptionPane.WARNING_MESSAGE);
String sqlstr1;
JdbcFiles conn=new JdbcFiles();
sqlstr1="select number,bookname,publish,author,isbn,price from book where convert(nvarchar(255),number)='"+number.getText()+"'";
ResultSet result=conn.executeQuery(sqlstr1);
if(result.next()){
{
bookname.setText(result.getString("password"));
publish.setText(result.getString("college"));
author.setText(result.getString("subject"));
isbn.setText(result.getString("grade"));
price.setText(result.getString("name"));
bookname.setEnabled(true);
publish.setEnabled(true);
author.setEnabled(true);
isbn.setEnabled(true);
price.setEnabled(true);
}
}
else
JOptionPane.showMessageDialog(null, "当前没有该书信息","修改",JOptionPane.WARNING_MESSAGE);
}catch (ClassNotFoundException ce) {
System.out.println("SQLException:" + ce.getMessage()); } catch (SQLException ex) {
System.out.println(ex); } catch (Exception s) {
s.printStackTrace();
} }
});
//为button2增加监听
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
String sqlstr2="update book set bookname='"+bookname.getText()+"',publish='"+publish.getText()+"',author='"+author.getText()+"',isbn='"+isbn.getText()+"',price='"+price.getText()+"' where convert(nvarchar(255),number)='"+number.getText()+"'";
int k=-1;
JdbcFiles conn1=new JdbcFiles();
k=conn1.insert(sqlstr2);
if(number.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "编号不能为空","修改",JOptionPane.WARNING_MESSAGE);
else
if(bookname.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "书名不能为空","修改",JOptionPane.WARNING_MESSAGE);
else
if(publish.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "出版社不能为空","修改",JOptionPane.WARNING_MESSAGE);
else
if(author.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "作者不能为空","修改",JOptionPane.WARNING_MESSAGE);
else
if(isbn.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "ISBN不能为空","修改",JOptionPane.WARNING_MESSAGE);
else
if(price.getText().toString().equals(""))
JOptionPane.showMessageDialog(null, "价格不能为空","修改",JOptionPane.WARNING_MESSAGE);
else if (k > -1)
{
JOptionPane.showMessageDialog(null, "修改成功!", "修改",JOptionPane.WARNING_MESSAGE);
conn1.close();
} else
JOptionPane.showMessageDialog(null, "修改失败!", "修改",JOptionPane.WARNING_MESSAGE);
}catch(ClassNotFoundException ce){
System.out.println("SQLException:"+ce.getMessage());
}catch(SQLException ex){
System.out.println(ex);
}catch(Exception s){
s.printStackTrace();
}
}
});
setSize(250,380);
this.setVisible(true);
}
public static void main(String[] args)
{
UpdateBook app1=new UpdateBook();
}
}
可能是你的列名写错了,要么不存在。
看看具体的报错信息