好吧,我有点卡在我必须问的地方
我有 2 个名为 main 和 dbconnect 的类
在我的 dbconnect 中,我拥有连接到数据库的所有必要方法,在我的主类中我创建了 GUI,但问题是我在主类中声明了变量(因为它们由文本框表示),现在想在我的 dbconnect 中使用它们班级
我的 dbconnect 课程如下所示
import javax.swing.*;
import java.sql.*;
public class DBCONNECT
{
Connection conn;
public void connect()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:DriverConn");
}
catch(Exception se)
{
JOptionPane.showMessageDialog(null, "Error: Could not load driver " + se.getMessage());
}
}
public void dissconect()
{
try
{
conn.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Error : Could not close Database " + e.getMessage());
}
}
public void writeToDB()
{
try
{
MAIN main = new MAIN();
Statement st = conn.createStatement();
String command1 = ("INSERT INTO Drivers (IDNumber, FirstName, LastName) VALUES" + drivID + FName + LName);
String command2 = "INSERT INTO Offences(IDNumber, SpeedLimit, DriverSpeed, SeatBelt, DrunkenDriving, DriversLicense) VALUES" + drivID + intspeed + intDSpeed + strSeat + strDrunk + strLicense;
String command3 = "INSERT INTO DriverPoints(IDNumber, Points) VALUES" + drivID + intpoints;
}
}
public ResultSet select(String sql)
{
try
{
Statement st = conn.createStatement();
return st.executeQuery(sql);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Error: Could not read from database");
}
return null;
}
public int change (String sql)
{
try
{
Statement st = conn.createStatement();
return st.executeUpdate(sql);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Error: Could not Update Database");
}
return -1;
}
}
有人可以帮忙吗?