"\\+"
instead of just
"+"
and likewise for the rest.
"\\+"
instead of just
"+"
and likewise for the rest.
There is a menu item somewhere under the browsers menus (called Java Console or something very close to that) and the printStackTrace will be printed there (if it occurs).
Don't ignore errors
catch(Exception e) { }
Add a e.printStackTrace() to that catch block and check the Java Console.
executeUpdate not executeQuery (on non-select queries)
I'm sorry, but how dense are you?
output.writeUTF((String) number.getSelectedItem());
It might help to know what you mean by "it didn't work" and "i get an error".
Also, did you pay attention to the fact that I said you needed to do some casting?
Also, simply go here, http://java.sun.com/javase/6/docs/, and you can look up the classes yourself. You should already have this link bookmarked anyway.
Edit: And the first link didn't work, because it looks like the closing paren ")" is included in the link, and it shouldn't be.
Insert the cd and reboot. (And make sure that cd comes before harddisk in your boot order.)
Well, what does writeUTF take as a parameter? (As a hint, here are the API docs for DataOutputStream http://java.sun.com/javase/6/docs/api/java/io/DataOutputStream.html).
And what does getSelectedIndex return? (As a hint, here are the API docs for JComboBox http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html.)
Now, what do you think is wrong?
Look closely at the docs for JComboBox. There is another getSelected.... method that will work (as long as the items in the JComboBox are Strings, and you cast the return Object properly) for you, find it.
What does the API documentation say about it?
They both say that the String will be evaluated as if by (and probably because it uses) valueOf from the respective Class. So, read the API docs for the valueOf methods of those classes and you will see what is, and what is not allowed. Double valueOf description even shows a regex you can use to "screen" input, in order to avoid a NumberFormatException.
This sort of question is always best answered using the API docs:
http://java.sun.com/j2se/1.5.0/docs/api/index.html
Actually, you may use either l or L when entering the number directly, but neither of them are to be used when entering a String.
And a quick test showed that both variants throw an exception. I think you're missing the quotes on the last example, in the actual code.
Edit: Either that, or the code simply does not attempt to execute the second, as it get an error on the first. They will both compile. Try reversing those two lines in your code (if you have them) and you should notice that the other throws an exception, too.
Yes, use setActionCommand to set the actionCommand to the counter's value, then use getActionCommand in the Listener to display it. That is hijacking the purpose of the actionCommand, a bit, but heh, nobody's perfect.
Not really... Because im using jdk1.5 on WinXp platform.
And what does that have to do with anything? The character used to denote a long is still L and not l.
Since you do not use setActionCommand("String"), the actionCommand will be based on the text shown on the button. Since they all have the same text (i.e. none) all of the Dialogs will be shown, since all of the ActionListeners (and you have a separate one for each button) loop through all the buttons.
That loop (inside the ActionListener) is completely unnecessary, as each button will have it's own ActionListener. Just simply have it perform whatever action you want it to perform, as the button to which it is added is the only thing that ActionListener reacts to.
Because a number, typed directly into the code (i.e a = 15), is an integer. If you want a short, or a byte, or a char you need to cast it.
Edit. Also, the "character" for long is L not l.
That is a legacy Driver. It is only around for backwords compatability. Use com.jdbc.mysql.Driver instead.
P.S. No need to change your jarfile (depending on the version of course), just change the String in the Class.forName() or Driver.registerDriver() method call.
You need to close your resultsets, statements, and connections (in every place you're using them).
God, I'm blind sometimes. Your error is not there (at least not this error). Your error is this
String[] serv=null;
serv[0]=String.valueOf(port_array[0]);//seending the port # of client who has the server socket
You set the String array serv to null, then attempt to access an index of that array, which will, of course, throw a nullpointerexception since serv is null.
Your error then, seemingly occurs in this method
"enterintodb"
as that is called before the other (and the output around it is not contained in your output).
Also, the stack trace is all the other information that comes with that nullpointerexception. One piece of that will actually point you to the exact line where the error occurs. Which line is that?
Then what is this
"Unable to connect to the database in authGeneral error"
You could start by using the correct class
javax/swing/plaf/metal/MetalLookAndFeel
There is a file called classlist in the lib directory of the JRE which contains a complete listing of the classes in the JRE.
The basis of your error, however, seems to be that you cannot connect to the DB. Are you sure that that the dsn is defined as "MS Access Database" as you have in your code.
If msk is the DSN (as your comment suggests), then use that.
server.lookupserver.main(serv);
Where is any of the above (aside from the String array serv) declared and/or defined.
According to your code, server is a package, so the only way this could possibly work, is if "lookupserver" were a class within server, and main a static method in that class.
Seemingly
com.sun.java.swing.plaf.metal.MetalLookAndFeel
doesn't exist.
Why don't you simply use String's split method and ignore the last resulting index?
http://java.sun.com/javase/6/docs/api/java/util/HashMap.html
Edit: P.S. The API docs are a wonderful thing. They answer all sorts of questions.
Use a map.
No, in all likelihood it is not, though allocating such variables in loops can lead to excess GC activity - and even in that case there is a good chance the JIT compiler will re-use a single reference instead of creating a new one each time anyway unless subsequent assignments make that impossible. The short answer is store the calculation in a local variable.
Actually, the space for the variables is allocated at the "method" level. So, declaring a variable before the loop, or in the loop, has no additional effect what-so-ever. ;-)
Edit: Doing an operation (of any kind) is always more expensive than simply accessing a variable, but, unless it is a very involved calculation, the difference is not enough to be noticed, unless it is being carried out millions or billions of times (such as in simulations, as I already said).
What do think? Is it more effecient to smithy a new knife every time you need one, or to use the same one again?
But pay attention to what you've already been told. The reusabilty and definately maintenance of the code should be the first priority. Usually, with today's processors, you don't have to worry about effeciency hardly at all (but only hardly, let's not get carried away, here), unless your doing simulations or something to that effect.
Yeah, take a look at the classes in the security (JSSE) package
http://java.sun.com/javase/6/docs/api/java/security/package-summary.html
You want to stop it from "hurt more comps" (not that I believe either wrote one, or that you want to stop it), go to the police with your virus and give them a detailed description of your first use of it.
If you want to use the decimal values, then there is no need for either a \ or ' characters, as the decimal ascii "escape code" is the same as the integer value for the char. So, simply do
char c = (char) 29;
Hi, thanks for the reply.
How the does the compiler converting it to equivalent char? is it finding some matching char for that or? I did look into ASCII table, but the character i find was different character from the one which above program prints.How does java interpreting '\21' and '\29'(for eg.) there are some other examples which compiles and some not. How to recognize given an example whether
char c='\21';
orchar c = '\29';
that java recognizes?.katharnakh.
See my post. Those are the octal and not decimal values. (I know, we probably "crossed" our replies).
It's because those are octal escape codes, and there are no 8s or 9s in octal numbers.
Edit:
Also a char is a single character. So '3' is okay, but '33' is two characters, so it is not okay.
Have added the JLabel to my container..
Did nt include that part of my code as it dint seem important..
What was about the "REFRESHING" that you mentioned..
I am sorry was unable 2 get it
call validate() on the component you added the label to.
Also, replace that
} catch (Exception e) {}
with
} catch (Exception e) {
e.printStackTrace();
}
Then tell us if any error occurs. Simply swallowing errors, like you are doing, is very, very, very, very bad practice.
Generally URLs are only used for the internet, use Files for local stuff...
True, to an extent, but it still works with a url, as it will be given a "file://" url, rather than an "http://" url. It is still valid. His problem lies either in that he is not adding the JLabel, at all, is not calling validate() on whatever he added it to, or, because he is calling "new JLabel" in the try block, he is not actually changing the JLabel that already appears in the GUI, but rather creating a new one and not doing anything with it. (I believe, anyway.)
Well, you are neither adding the JLabel to anything, nor "refreshing" whatever you might have wanted to add it to.
Edit: And, if you want to change a Lable that already exists somewhere in the GUI, then don't use new JLabel() in the try/catch block. If you do, you have created a new JLabel, but the original still exists and is still a part of the GUI.
If they are coming from the same statement, no. The API clearly says that any time a statement executes a query, all previous resultsets will be closed. Multiple statements on the same connection will allow you to have multiple resultsets. One per statement. However, as has been pointed out, if you are using the JDBC-ODBC bridge you may only have one statement at a time.
What DB are connecting to, and what Driver are you using?
that would be one resultset per statement, not per connection.
Yep. Hate those little (BIG) mistypes. ;-)
You can have multiple statements on one connection (though only one resultset per connection, normally), but seemingly, your connection can only have one statement active at the same time. Are you, per chancce, using the JDBC-ODBC Bridge, if so take a look at this:
http://java.sun.com/products/jdbc/faq.html#15
Reflection (to see if the method exists). Search for it. But I haven't got a clue what kind of a screwed up design you have.
Also, with an interface, you do not need to know the name of the class of the object being passed in, but whoever wrote that class needs to have either implemented the interface, or extended the base class. Your method need know nothing more about it.
I makes absolutely no sense to just assume that an Object will have a specific method when there is no prior defintion of the method. And that is exactly what the compiler is telling you with that error message.
Because, the only methods that all objects have are those methods defined in Object.
What you want to do, doesn't exist. If want something at least a little bit like this, then read reply #5 again (the part about abstract classes and interfaces).
So?
What are they? Try to fix them as well.
I think, masijade, that he wants to call this method:whateverMethod(Object obj) with any kind of object and for the method to do what is necessary every time dynamically. Meaning that I don't think that he intends to check what type of class he is passing in order to do casting.
Perhaps if he wants to save the object he is passing, he should consider serializing/desirializing
My "solution" is the same thing, simply "widened" with varargs so that the number is not limited to only one. That is, of course, even worse, but the principle in both remain the same, as well as my last statement. The need to do this is usually (not always, but usually) a result of bad design. The very least he could do is to create a base abstract class to inherit from, or an interface to implement, and use that abstract class or interface as the parameter. That, at least, dictates some restriction.
What are you talking about? Send a parameter dynamically? No idea what you mean there.
However, why don't you just make the defintion as follows:
public whatever whateverMethod(Object... arguments) {
// whatever
}
Now you can pass it as much of anything as you like.
The necessity for something like this, however, means you have a bad design and need to rethink your application design.
You invalidate the session. I would have to look up how to do it in JSP myself, but if your using a servlet, or a filter to accomplish the logout, then that is going to be Java code, and there it is
yourHttpSessionVariable.invalidate();
I hope your not starting every page with the equivalent of
HttpSession session = request.getSession();
// or
HttpSession session = request.getSession(true);
as those create a new session if no valid session already exists. That method should only be used on the initial login every other should use
HttpSession session = request.getSession(false);
this one will return null if there is no valid session.
The best place to do this is in a Filter, as there you can redirect the user to a login site (when no valid session exists), and once the user is logged in the Filter can then redirect back to the site the user originally wanted.
Well, nothing is fool proof.
You need to use the no-cache and expires pragmas/headers, and use some JavaScript to wipe out the history on the browser (Google "javascript history", although this might also be deactivated).
But, no matter what you do, you need to have a session check on each and every page (easiest to use a filter) so that the user, at least, can't do anything on the pages.
Well, is this an update or an insert? Your query was a bit mixed last time, and I created an insert out of it (moving ID into the column and value list). If you simply removed ID, then there are only three fields, and that it why it complains on #4. If you want an update then your query is all wrong, and should look like this:
PreparedStatement ps = conn.prepareStatement("Update Questions Set Question = ?, Answer = ?, WhateverYouCallItNow = 'this has been updated', Author = ?, Customer_Useful = 0, Customer_NotUseful = 0 Where ID = ?");
ps.setString(1, Question);
ps.setString(2, Answer);
ps.setString(3, Author);
ps.setString(4, ID);
You really need to read through both the JDBC tutorial, and an SQL tutorial before continuing.
Date is also a reserved word, and you need to look at the "edited" version of the post, as that Where clause is not something that belongs in an insert statement.
Edit: And take a careful look at the PreparedStatement as it appears in my post, and as it appears in yours. You haven't not excahnged any of those cobbled areas of the statements (i.e. the '" + var + "' areas) with the question mark placeholders.
I have a JSP which inserts date into a MS Access database. But I keep receiving the error:
24: statement.executeUpdate("INSERT INTO Questions WHERE ID ='"+ID+"'(\"Question\", \"Answer\", \"Timestamp\", \"Author\", \"Customer_Useful\", \"Customer_NotUseful\") VALUES ('"+Question+"','"+Answer+"','this has been updated','"+Author+"','0','0')");
I have checked and checked all the values are there and correct and that the database is expecting to receive the correct types. I'm sure its just a comma or something. Thanks for your help!
Use a PreparedStatement, rather than a cobbled together Statement (which could contain bad values in the variables), and this probably usually won't happen.
statement.executeUpdate("INSERT INTO Questions WHERE ID ='"+ID+"'(\"Question\", \"Answer\", \"Timestamp\", \"Author\", \"Customer_Useful\", \"Customer_NotUseful\") VALUES ('"+Question+"','"+Answer+"','this has been updated','"+Author+"','0','0')");
Should be (as you have it)
statement.executeUpdate("INSERT INTO Questions (ID, Question, Answer, \"Timestamp\", Author, Customer_Useful, Customer_NotUseful) VALUES ('"+ID+"', '"+Question+"','"+Answer+"','this has been updated','"+Author+"',0,0)");
Assuming that the last two fields are number fields. The "TimeStamp" column needs quotes because that is a reserved word in nearly all DBs. You should change that column name. Also, if either question, or answer, has "special" characters (such as an apostrophy), the insert will fail (unless you do some leg work to prevent it that PreparedStatement does for you).
Better, your statement should be like this:
PreparedStatement ps = conn.prepareStatement("INSERT INTO Questions (ID, Question, Answer, \"Timestamp\", Author, Customer_Useful, Customer_NotUseful) VALUES (?, ?, ?,'this has been updated',?,0,0)");
ps.setString(1, ID);
ps.setString(2, Question);
ps.setString(3, Answer);
ps.setString(4, Author);
ps.executeUpdate();
This prevents SQLinjection attacks and it automatically properly escapes all special characters in the values used. You should …
Set a socketTimeout on the ServerSocket and place the accept in a while loop so that it will "interrupt" every once in a while.
I.E.
ServerSocket ss = .....
ss.setSoTimeout(1000);
while(true) {
Socket s = ss.accept();
// do whatever you normally do with s (if it is defined);
// check whatever you want to check to possibly reassign your ServerSocket and then do so
}