我是Riya a B.Tech。學生,我必須在主題大學管理系統上做一個corejava項目。我已經使用MS Access創建了所需的數據庫。我在我附上的代碼中遇到問題。實際上我想更新數據庫中的數據,所以我在其中一箇中創建了2個框架。被問到什麼時候輸入,然後第二幀打開,並要求輸入標記更新...當我輸入要更新的標記時,對話框打開「標記更新成功」。直到這裏我沒有任何問題..問題是當我打開數據庫時,我看到實際上標記沒有更新那裏。所以請請有人幫我解決這個問題。corejava需要項目幫助
的代碼如下:
1幀
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
class MyFrame03 extends JFrame implements ActionListener {
JLabel l1;
JTextField t1;
JPanel p1, p2;
JButton b1;
Connection con;
PreparedStatement pst;
String s1;
MyFrame03() {
setLayout(new GridLayout(4, 1));
setTitle("Enter Data Required");
setBackground(Color.blue);
l1 = new JLabel("Roll no");
t1 = new JTextField(12);
p1 = new JPanel();
p2 = new JPanel();
b1 = new JButton("SUBMIT");
p1.add(l1);
p1.add(t1);
p2.add(b1);
add(p1, "Center");
add(p2, "South");
b1.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 500);
setVisible(true);
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("Jdbc:Odbc:dsn3");
if (con != null)
System.out.println("Connection Established");
}
catch (Exception e) {
System.out.println("exception");
}
}
public void actionPerformed(ActionEvent e1) {
if (e1.getSource() == b1) {
s1 = t1.getText();
try {
pst = con.prepareStatement("insert into stud values(?)");
pst.setString(1, s1);
pst.executeUpdate();
con.commit();
con.close();
}
catch (SQLException e) {
System.out.println("except1");
}
MyFrame04 m1 = new MyFrame04(s1);
dispose();
}
}
public static void main(String s[]) throws SQLException {
MyFrame03 m1 = new MyFrame03();
}
}
2幀
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
class MyFrame04 extends JFrame implements ActionListener {
JLabel l1;
JTextField t1;
JPanel p1, p2;
JButton b1;
String s1, s2;
Connection con;
PreparedStatement pst;
public MyFrame04(String s1) {
this.s1 = s1;
setLayout(new GridLayout(4, 1));
setTitle("Update Marks");
setBackground(Color.blue);
l1 = new JLabel("Enter Marks");
t1 = new JTextField(12);
b1 = new JButton("SUBMIT");
p1 = new JPanel();
p2 = new JPanel();
p1.add(l1);
p1.add(t1);
p2.add(b1);
add(p1, "Center");
add(p2, "South");
b1.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 500);
setVisible(true);
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("Jdbc:Odbc:dsn3");
if (con != null)
System.out.println("Connection Established");
}
catch (Exception e) {
System.out.println("exception");
}
}
public void actionPerformed(ActionEvent e1) {
if (e1.getSource() == b1) {
s2 = t1.getText();
try {
pst = con.prepareStatement("UPDATE stud set Marks=? WHERE Roll no=?");
pst.setString(1, s2);
pst.setString(2, s1);
pst.executeUpdate();
con.commit();
con.close();
}
catch (SQLException e) {
System.out.println("except1");
}
JOptionPane.showMessageDialog(this, "Marks updated succesfully");
dispose();
}
}
}
三江源
我會盡力回顧一下,但如果你能總結或簡化問題,它確實能幫助我們所有人。很難確切地說出你在問什麼。 – 2010-09-07 14:17:39
如果這是一個班級作業,您應該添加作業標籤。 – trashgod 2010-09-07 14:41:41
好吧我會添加標籤,但請幫我解決這個問題。我的問題是,請檢查2幀的編碼,如果有什麼問題,請告訴我ñ幫助我。 – user677814 2010-09-07 15:08:01