hi i am trying to get the local ip address and then update and add it to my database table has a string type when my gui first opens, but it does not seem to work.
i can display the ip address and a username logged into my program via alert messege box but it cant seem to query those values and update my table..
anyway here is the class for my queries followed by my class for the gui..
package icomm;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
/**
*
* @author jonathan
*/
public class MySQL_queries
{
private Statement stmt;
private ResultSet rs;
private String url = "jdbc:mysql://192.168.0.3:3306/i-comm";
private String url2 = "jdbc:mysql://wavelength.mine.nu:3306/i-comm";
private String db_user = "guest";
private String db_pass = "guest";
private String driver = "com.mysql.jdbc.Driver";
/** Creates a new instance of MySQL_queries */
public MySQL_queries()
{
}
public void find_ip(String user_name1, String ip1)
{
try
{
Class.forName(driver);
Connection con = DriverManager.getConnection( url,db_user, db_pass);
//find username and set their ip address
String find = "UPDATE users SET ip = ? WHERE Username = ?";
PreparedStatement ps1 = con.prepareStatement(find);
ps1.setString(1, user_name1);
ps1.setString(2, ip1);
ps1.executeUpdate();
con.close();
}
catch( Exception e )
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, "error, cannot assign ip address", null, JOptionPane.ERROR_MESSAGE);
}
}
public void off_status(String name)
{
try
{
Class.forName(driver);
Connection con = DriverManager.getConnection( url,db_user, db_pass);
String off ="UPDATE users SET Status = ? WHERE Username = ?";
PreparedStatement ps = con.prepareStatement(off);
ps.setString(1, "Offline");
ps.setString(2, name);
ps.executeUpdate();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, "error, cannot connect or add data into the database", null, JOptionPane.ERROR_MESSAGE);
}
}
public void reset_ip(String name1)
{
try
{
Class.forName(driver);
Connection con = DriverManager.getConnection( url,db_user, db_pass);
String ip ="UPDATE users SET IP = ? WHERE Username = ?";
PreparedStatement ps = con.prepareStatement(ip);
ps.setString(1, "hello");
ps.setString(2, name1);
ps.executeUpdate();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, "error, cannot connect or add data into the database", null, JOptionPane.ERROR_MESSAGE);
}
}
}
////////////////////////////////////////////////////
as you can see, the find_ip method suppose to find the username based on who was logged in and then add the ip address to the table. i do not get any errors when running the program...
here below is my gui.
/*
* user_window.java
*
* Created on 10 February 2006, 11:50
*/
package icomm;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
/**
*
* @author jonathan
*/
public class user_window extends javax.swing.JFrame implements WindowListener {
protected String crnt_user;
protected String crnt_ip;
/** Creates new form user_window */
public user_window() {
initComponents();
addWindowListener( this );
}
public user_window(String users_name)
{
initComponents();
addWindowListener( this );
this.crnt_user = users_name;
}
private void exit()
{
MySQL_queries offline = new MySQL_queries();
offline.off_status(crnt_user);
JOptionPane.showMessageDialog(null, "you are about to close the program " + crnt_user, null, JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
public void windowClosing(WindowEvent e)
{
exit();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
try {
Buddie_list =(javax.swing.JTree)java.beans.Beans.instantiate(getClass().getClassLoader(), "icomm.user_window_Buddie_list");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
}
label = new javax.swing.JLabel();
demo = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
Close = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
profile = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(Buddie_list, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 230, 310));
getContentPane().add(label, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 390, -1, -1));
demo.setText("talk to shienna");
demo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
demoActionPerformed(evt);
}
});
getContentPane().add(demo, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 360, -1, -1));
jMenu1.setText("File");
jMenu1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu1ActionPerformed(evt);
}
});
Close.setLabel("Close");
Close.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CloseActionPerformed(evt);
}
});
jMenu1.add(Close);
jMenuBar1.add(jMenu1);
jMenu2.setText("Option");
profile.setText("Edit Profile");
profile.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
profileActionPerformed(evt);
}
});
jMenu2.add(profile);
jMenuBar1.add(jMenu2);
jMenu3.setText("Help");
jMenuBar1.add(jMenu3);
setJMenuBar(jMenuBar1);
pack();
}
// </editor-fold>
private void demoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void CloseActionPerformed(java.awt.event.ActionEvent evt) {
MySQL_queries query = new MySQL_queries();
query.off_status(crnt_user);
query.reset_ip(crnt_user);
}
private void locate_ip()
{
try
{
InetAddress a;
a = InetAddress.getLocalHost(); //get ip addrees
this.crnt_ip = a.getHostAddress(); //then store it in this variable
}
catch(UnknownHostException e)
{
JOptionPane.showMessageDialog(null, "Error, cant detect localhost", null, JOptionPane.ERROR_MESSAGE);
}
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e)
{
locate_ip();
MySQL_queries new_ip = new MySQL_queries();
new_ip.find_ip(crnt_user, crnt_ip);
JOptionPane.showMessageDialog(null,"hello " + crnt_user + " your ip is" + crnt_ip, null, JOptionPane.ERROR_MESSAGE);
}
private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void profileActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new user_window().setVisible(true);
}
}
);
}
// Variables declaration - do not modify
private javax.swing.JTree Buddie_list;
private javax.swing.JMenuItem Close;
private javax.swing.JButton demo;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JLabel label;
private javax.swing.JMenuItem profile;
// End of variables declaration
}