Reputation: 197
Okay I'm kinda stuck at a point where I have to ask
I have 2 classes called main and dbconnect
In my dbconnect I have all the neccesary methods to connect to a database and in my main class I created the GUI, but problem is I declared variables in my main ( because they are represented by textboxes) and now want to use them in my dbconnect class
my dbconnect class looks like follow
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;
}
}
can someone please help?
Upvotes: 0
Views: 771
Reputation: 534
A common approach for doing this would be either to pass each piece of information through to the dbconnect method from Main, or to create an object that holds the data and pass this into the dbconnect method. You can then access what you need through getters. You should not really reference Main directly from dbconnect as it introduces cyclical dependency.
Upvotes: 0
Reputation: 2257
Put both the classes in same folder and use the code for putting them in a same pakage.
package <package_name>;
then you can use the public and protected variables in the main class.
Upvotes: 1
Reputation: 9307
It is very easy to pass the required variables as parameters of method call. ie your writeToDB() method will be like writeToDB(String drivId, ...).
However, if you have additional logic like say you want to validate if drivId is in correct format etc. Then you should create a separate class for holding the attributes you want to save in database. So from your text widgets you will update this object, then call validate method, etc and then pass it to database handler. This will isolate model related data from the view.
Upvotes: 2
Reputation: 470
Would it be possible to make those variables public and static? You could then use them by referencing them using the Main class.
Ex:
Main.drivlD
Upvotes: 1
Reputation: 10417
Either declare the variables public
or create Accessor Methods for the variables in the main class. You will need to pass the main class instance to the DBCONNECT
class so that is has a reference to it.
Upvotes: 2