Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DIRE STRAIGHTS -- NEED HELP ASAP!!!

Status
Not open for further replies.

ceaton

MIS
Apr 26, 1999
27
US
I'm in need of some help rather fast, project due!!<br>
<br>
I am so new to java and I have this project due and the boss is breathing down our necks, anyway I need to know how to call some classes. I have this button on an applet from which I need to trigger the following class. <br>
<br>
void va_MouseClicked(java.awt.event.MouseEvent event)<br>
{<br>
I need to pass a string or label to my class.<br>
}<br>
<br>
<br>
This is my query class, now I need to make several of these, I'm just not sure how to put all three of my class to work together. I think I'm really missing something here, just not sure what.<br>
<br>
<br>
import java.awt.*;<br>
import java.io.*;<br>
import java.sql.*;<br>
import java.net.URL;<br>
<br>
public class BRR extends Object {<br>
JVRConn conn; <br>
<br>
public BRR(JVRConn _conn) {<br>
<br>
conn = _conn;<br>
<br>
String specLiab; <br>
PreparedStatement prepstmt;<br>
<br>
// the labelspecliab is what I need to retrieve from my button,<br>
// I can either pass this as a label or string.<br>
<br>
specLiab = ces.labelspecliab.getText().toString();<br>
<br>
try {<br>
boolean found = false;<br>
prepstmt = JVRConn.theConn.dbConn.prepareStatement<br>
(&quot;SELECT BRR FROM LIAB WHERE TEXT = ?&quot;);<br>
prepstmt.setString(1, specLiab); <br>
<br>
<br>
ResultSet rs;<br>
rs = prepstmt.executeQuery();<br>
<br>
found = rs.next();<br>
if (found)<br>
System.out.println(rs.getString(1));<br>
else<br>
System.out.println(&quot;Not here&quot; + specLiab + &quot;);<br>
prepstmt.close();<br>
}<br>
catch(Exception e ) {<br>
e.printStackTrace();<br>
}<br>
}<br>
<br>
}<br>
<br>
This is my class to make the connection to my db. I know this is correct. I can't just put them together.<br>
<br>
import java.net.URL;<br>
import java.sql.*;<br>
<br>
class JVRConn {<br>
static myConnection theConn;<br>
<br>
public static void main (String args[]) {<br>
theConn = new myConnection();<br>
theConn.Connect2Db(&quot;JVR&quot;, &quot; &quot;, &quot; &quot;);<br>
} <br>
}<br>
<br>
class myConnection {<br>
Connection dbConn = null;<br>
void Connect2Db(String db, String user, String passw) {<br>
try {<br>
// using the driver<br>
Driver d = <br>
(Driver)Class.forName<br>
(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;).newInstance();<br>
// URL corresponding to the ODBC DSN<br>
String URL = &quot;jdbc:eek:dbc:&quot; + db;<br>
// DB logon<br>
dbConn =<br>
DriverManager.getConnection(URL, user, passw); <br>
}<br>
catch (Exception e) {<br>
e.printStackTrace();<br>
}<br>
}<br>
<br>
void Disconnect2Db() {<br>
try { <br>
dbConn.close(); <br>
}<br>
catch (Exception e) {<br>
e.printStackTrace();<br>
}<br>
}<br>
} <p> Courtney<br><a href=mailto: ceaton@lrp.com> ceaton@lrp.com</a><br><a href= > </a><br>
 
First, here is my understanding of your situation:<br>
<br>
You have some sort of gui/applet with a button (or set of) that each has a label. When the user clicks on one of these buttons, you want to receive the button pressed event and create the SQL statement using the text of the label of the button that was pressed. Also, since you only include java.awt.*, I am assuming you are not using Swing.<br>
<br>
<br>
First you do not need a MouseClicked routine. It wouldn't help you anyway since mouse clicks are registered on the gui and not just over a button. Second, don't inherit your class from java.lang.Object because ALL Java objects automatically inherit from Object. <br>
<br>
Well given the structure of your code, it would probably be easiest to make your BRR class implement java.awt.event.ActionListener. A class which implements ActionListener signals that it is willing to handle GUI events. For this to function, you need to implement the function &lt;public void actionperformed(java.awt.event.Event e)&gt; in your BRR class.<br>
<br>
Then wherever you create your GUI (I don't see any GUI code here), set the action listener for the button (button.addActionListener(&lt;the object which implements ActionListener&gt;). This of course means that you will have to move the SQL Connection code out from the constructor to another function and call it once you have the button name.<br>
<br>
I wrote a small, simple GUI which shows how you can create a button and set the action listener.<br>
<br>
*********************************<br>
import java.awt.*;<br>
import java.awt.event.*;<br>
<br>
<br>
public class ClickMe implements ActionListener {<br>
<br>
Frame frame;<br>
TextArea area;<br>
<br>
public ClickMe() {<br>
frame = new Frame();<br>
area = new TextArea();<br>
<br>
Button button = new Button(&quot;ClickMe&quot;);<br>
<br>
button.addActionListener(this);<br>
<br>
frame.add(button, BorderLayout.NORTH);<br>
frame.add(area, BorderLayout.SOUTH);<br>
<br>
frame.addWindowListener(new WindowAdapter() {<br>
public void windowClosing(WindowEvent e) {<br>
System.exit(0);<br>
}<br>
});<br>
<br>
frame.pack();<br>
frame.setVisible(true);<br>
}<br>
<br>
<br>
public void actionPerformed(ActionEvent e) {<br>
area.setText(e.getActionCommand() + &quot;\n&quot;);<br>
}<br>
<br>
public static void main(String[] args) {<br>
ClickMe c = new ClickMe();<br>
}<br>
}<br>
*********************************<br>
<br>
This compiles with JDK1.1 and only uses AWT components. <br>
<br>
Hoepfully this can get you started in the right direction.<br>
<br>
tito<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top