我是java编程的新手,需要帮助从另一个jform类调用java方法。我基本上需要将名为Prizes.java的第二个jform连接到MySQL数据库并调用一个方法(可能是Fillforms?),以便编辑MySQL表中的同一行数据(由" ID" )。我厌倦了在Fillforms上设置返回值但看起来它不适用于try方法。所以我不确定我是否需要制作新方法,还是可以更改我的方法? Tickets.java
package Tickets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import java.sql.*;
public class Tickets extends javax.swing.JFrame {
Connection con;
Statement stmt;
ResultSet rs;
int curRow = 0;
public Tickets() {
initComponents();
DoConnect();
}
public void DoConnect( ) {
String url = "jdbc:mysql://&&.&&.&&.&&&:3306/";
String dbName = "pos";
String driver = "com.mysql.jdbc.Driver";
String userName = "Switcher";
String password = "*****";
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+dbName,userName,password);
//conn.close();
stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
String sql = "SELECT * FROM instock";
rs = stmt.executeQuery(sql);
rs.next();
Fillforms();
}
catch (Exception e) { e.printStackTrace(); }
}
public void Fillforms(){
int id_col = 0;
try{
id_col = rs.getInt("ID");
String id = Integer.toString( id_col );
String game_name = rs.getString("Game");
String mf_id = rs.getString("MF_ID");
String dist_id = rs.getString("Dist_ID");
String part_no = rs.getString("Part_No");
String game_type = rs.getString("Game_Type");
String inv_num = rs.getString("Invoice_num");
String date_purch = rs.getString("Date_purch");
String date_placed = rs.getString("Date_placed");
String date_removed = rs.getString("Date_removed");
String inplay = rs.getString("Inplay");
String ticket_cost = rs.getString("Ticket_cost");
String num_tickets = rs.getString("Num_tickets");
String ideal_gross = rs.getString("Ideal_gross");
String ideal_prizes = rs.getString("Ideal_prizes");
String ideal_net = rs.getString("Ideal_net");
String game_cost = rs.getString("Game_cost");
String unsold_amt = rs.getString("Unsold_amt");
String actual_gross = rs.getString("Actual_gross");
String actual_prizes = rs.getString("Actual_prizes");
String actual_net = rs.getString("Actual_net");
String bin = rs.getString("Bin");
String last_sale = rs.getString("Last_Sale");
String unsold_tick = rs.getString("Unsold_tickets");
String serial = rs.getString("Serial");
textGame.setText(game_name);
textID.setText(id);
textSerial.setText(serial);
textPartNum.setText(part_no);
textManID.setText(mf_id);
textDistID.setText(dist_id);
textGameType.setText(game_type);
textInvoiceNum.setText(inv_num);
textInvoiceDate.setText(date_purch);
textDatePlaced.setText(date_placed);
textDateRem.setText(date_removed);
textCostTicket.setText(ticket_cost);
textNumTickets.setText(num_tickets);
textIdealGross.setText(ideal_gross);
textIdealPrizes.setText(ideal_prizes);
textUnsoldValue.setText(unsold_amt);
textActualGross.setText(actual_gross);
textActualPrizes.setText(actual_prizes);
textActualNet.setText(actual_net);
textBin.setText(bin);
textUnsoldTickets.setText(unsold_tick);
textIdealNet.setText(ideal_net);
textGameCost.setText(game_cost);
} catch (SQLException err) {
JOptionPane.showMessageDialog(Tickets.this, err.getMessage());
}
}