add = new JButton("Add",new ImageIcon("D:/Android/Assignment/Images/bookAdd.png"));
add.setToolTipText("Click Add to Store in Database"); // for toast
add.setBackground(new Color(233,214,199));
add.setForeground(new Color(207,108,40));
add.setFont(b);
add.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
String subject=tf2.getText();
String title=tf3.getText();
String author=tf4.getText();
int total_books=Integer.parseInt(tf5.getText());
try
{
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
System.out.println("11");
String sql="SELECT * from ADD_BOOKS WHERE (SUBJECT=?) AND (BOOK_TITLE=?)AND (BOOK_AUTHOR=?)";
// String sql="SELECT * from ADD_BOOKS VALUES(?,?,?)";
PreparedStatement pst=con.prepareStatement(sql);
// Statement stmt = con.createStatement();
System.out.println("12");
pst.setString(1, subject);
System.out.println("13");
pst.setString(2, title);
System.out.println("14");
pst.setString(3, author);
System.out.println("15");
ResultSet rs=pst.executeQuery();
System.out.println("16");
// if (rs! = null)
// {
while(rs.next())
System.out.println("17");
{
System.out.println("18");
String subjj=rs.getString(1);
String titlee=rs.getString(2);
String authorr=rs.getString(3);
int totalBooks=Integer.parseInt(rs.getString(4));
System.out.println("19");
if((subject.equals(subjj)) && (title.equals(titlee)) && (author.equals(authorr)))
{
System.out.println("20");
totalBooks=totalBooks+total_books;
String updateTableSQL = "UPDATE ADD_BOOKS SET TOTAL_BOOKS = TOTAL_BOOKS+totalBooks";
// + " WHERE SUBJECT = ?";
PreparedStatement pstup=con.prepareStatement(updateTableSQL);
// pstup=con.prepareStatement(updateTableSQL);
pstup.setInt(4, totalBooks);
pstup.executeUpdate();
JOptionPane.showMessageDialog(null,"Successfully Updated");
}
else
{
try
{
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con1=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
PreparedStatement prep1 = con1.prepareStatement("INSERT INTO ADD_BOOKS VALUES(?,?, ?, ?)");
prep1.setString(1, subject);
prep1.setString(2, title);
prep1.setString(3,author);
prep1.setInt(4,total_books);
prep1.executeUpdate();
String nextRowId = Integer.toString(dm.getRowCount());
dm.addRow(new Object[] {
nextRowId,
tf2.getText(),
tf3.getText(),
tf4.getText(),
tf5.getText()
});
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
System.out.println("Data Stored Into JTable Successfully");
JOptionPane.showMessageDialog(null, "Data is successfully inserted into database");
}
catch(Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, "error in submitting data");
}
}
}}
catch(Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, "error in submitting data");
}
}});
答案 0 :(得分:2)
建议 - 即使您只有一行代码,也总是使用大括号。在您的情况下,您没有这样做,然后您添加了在while循环中执行的System.out,然后您尝试在已经检索数据时获取数据。
这样做:
while (rs.next()) {
System.out.println("17");
System.out.println("18");
//...
}