Waldemar Macijewski

Ranch Hand
+ Follow
since Jun 22, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Waldemar Macijewski

Hi

You do not generate any exceptions, by using try and catch keywords you monitor piece of code which may create an exception and by using catch, you can "catch" this exception and handle it in some appropriate way. If you are referring to try with resources statement, then in try clause you declare and initialize objects whose resources are automatically managed. For example, if you create an instance of FileOutputStream, then any resources which are associated with this object (such as files in this case, but there also may be other resources such as network sockets, etc) are automatically closed. There is no need to call close() method in finally block.

for example:



12 years ago

Jonathan Wentworth wrote:
1). What's the purpose of javax.swingUtilities.invokeLater()?

2). Why does java need a main method? Why does it take String[] args? How come you can make classes without a main method if it's so necessary?



Hi.

1. The purpose of static methods invokeLater(Runnable obj) and invokeAndWait(Runnable obj) is to initialize GUI on the event dispatching thread. Event dispatching thread - is a separate thread created when user interacts with one of your applications components. The difference between invokeLater(Runnable obj) and invokeAndWait(Runnable obj) is that invokeLater(Runnable obj) return immediately, and invokeAndWait(Runnable obj) waits until obj.run() returns. The run() method is an entry point for the thread.

2. Well like in any other programming languages, every program needs to have it's own "entry point" to start. JVM needs to know what to execute first when your program starts? right? So that's what main method is for. The array of String objects simply represents any command line arguments which may be "passed" to your program when it starts, like java myprogram somearguments. Not every class needs to have it's own main method, you can make hundreds of classes, but eventually one of them will need to be your "main" class which has a main method defined.

P.S By any means I'm no expert, so you may need to wait for a better answer, but you can find such information easily by yourself in javadocs.
12 years ago
Correct me If I'm wrong:

A "stream" is a logical entity which either produces or consumes information. A stream can be linked to any physical device, memory, file, or a network socket - they all behave the same in Java I/O system. There are two types of streams, byte streams and character streams, they are all built upon four abstract classes: InputStream, OutputStream, Reader and Writer. In this case, FileInputStream is a InputStream which indicates that a stream from which you will be reading information is a file in a hard disk. The same applies to all subclasses of OutputStream, to what stream you will be writing information.
12 years ago
In Eclipse, you can add various libraries to your project in Build Path panel. In Package explorer right click on your project's name, Buildpath->Configure Build Path->Libraries->Add external jars. Point this to driver's .jar file.
Hi.

I don't know if this might solve your problem, but there is a feature called Serialization in Java. Serialization is, basically, a feature which allows you to save object's state into persistent storage area such as memory, or a file. So you could for example, save that variable's value into a file using serialization, and later retrieve whats necessary from that file.
Nah there was problem in the code after all. InputSource constructor should point to fully qualified path name of file, not just "Data.xml" how I did. I didn't see this because, i haven't handled all exceptions (in ChatServlet.java) which might occur when parsing the file, and when I did this, I saw that there was IOException all the time.
12 years ago

Seetharaman Venkatasamy wrote:

my wild guess is that SAXHelper.getDriver() is null


Well yes it is, but I still don't understand why? I have copied content of SAXHelper.java and used it with ordinary Java application, and I have tried exactly the same connect to database, and it worked fine..

12 years ago
I thought the problem was with the server for sure!. Anyway here are some code samples from these files:

ChatServlet.java (please ignore the name, it has nothing do with any chat, this just comes from my "rough notes" folder, so I just usually came up with any name)



SAXHelper.java



I have omitted some lines of code as they contain irrelevant information. Also I have placed "Data.xml" file in /webapps/ChatServlet/ folder.
12 years ago
Hi. I don't know if this is the appropriate place to post because I still consider this as a beginners question., but still the problem is servlet related. I have two classes ("ChatServlet.class" and "SAXHelper.class") which reside in package called "wm.macijewski". The servlet application ("ChatServlet.class") basically attempts to connect to database and retrieve some values, "SAXHelper.class" is a class which uses SAX API to retrieve some values from .XML document (as you might guess, these values are necessary information to connect to database). I have compiled these files with the following compiler options: "javac -d . SAXHelper.java" and javac -cp "/home/okti/apache-tomcat-7.0.32/lib/servlet-api.jar":"/home/okti/workspace/ChatServlet/src/wm/macijewski/" ChatServlet.java.

Copied those two .class files into "/home/okti/apache-tomcat-7.0.32/webapps/ChatServlet/WEB-INF/classes/wm/macijewski/". My WEB.xml file looks like this:


When i try to execute the servlet, I get:

java.lang.NullPointerException
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:186)
wm.macijewski.ChatServlet.createUser(ChatServlet.java:104)
wm.macijewski.ChatServlet.doGet(ChatServlet.java:80)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

I'm pretty sure the problem is, that server cannot initiate (or find) my class. But why? For me, it looks like I have placed them in correct directories, that is: /classes/wm/macijewski, and if the server fails to find my class, so which one? is it ChatServlet.class or SAXHelper.class? Also, I don't think the problem is within my code, because I have tried doing this with "normal" applications and it works fine.

Any Help is greatly appreciated!
12 years ago

Martin Vajsar wrote:You cannot use a bind variable (the ? character) to represent a table or a column name. They can be only used to replace a literal, and table names (as well as other identifiers and keywords) are not literals.

If it was possible to use bind variables to specify table names or keywords or other identifiers, using PreparedStatement would not prevent SQL injection. So I'd even say that no serious database allows it.



Live and learn. I have read so many tutorials regarding JDBC, and yet none of them mentioned a thing that you cannot use "?" placeholder for table and column names (or maybe it's something I missed). Anyway, if it wouldn't be for you, I would never know. Thank you, this solved my problem.
Yes I understand it's kinda of difficult to read, I tried making it more simple SQL statement (without breaking it into more strings, and I actually tried this before), but still no success. I got the same error message..
Hi all, I'm having a bit of trouble with this little program. I have two methods, first which connects to database as a root user and creates another user with the name supplied in the "username" variable. This works, as when I login to mysql shell and look into "mysql.user" table all the results are displayed correctly. Second method attempts to connect database as newly created user, and create some database, table and provide some bogus data. The error says I have bad SQL syntax, but I checked it and it seems fine. At first I thought I have misplaced placeholders (?), but if add them with or without "+" operator - doesn't make any difference. Since I'm using MySQL, and almost every statement requires semicolon at the end, I though adding ";" would help, but nope. Also when using INSERT statement we usually use "''" characters when giving values, that is:
INSERT into SomeTable VALUES (1, 'SomeValue'); I don't have "''" characters in my SQL statement, but if I add them or not, it sill doesn't have any effect.

Man I'm really getting frustrated here...



StackTrace:

Error: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Vadzik'use 'Vadzik'CREATE TABLE 'Contacts' (ID INTEGER, NAME VARCHAR(220));INSE' at line 1
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Vadzik'use 'Vadzik'CREATE TABLE 'Contacts' (ID INTEGER, NAME VARCHAR(220));INSE' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2734)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
at CreateDB.connectAsUser(CreateDB.java:31)
at CreateDB.main(CreateDB.java:116)


Anayonkar Shivalkar wrote:Hi Okti,
It is still unclear to me what exactly are you doing, and why do you need to convert String into array of byte, and then convert each byte into char.


Hey thanks for your time. What I'm trying to do, is convert String into byte array, and send this array to servlet. Now the servlet, will have to process this data, and find those Strings that I want. I'm note sure, how can you do that, but I thought since you can cast byte into char, I thought LinkedList<Character> would be appropriate data structure, and then using methods like "it.next()" extract what I need.

When I run my program, I see:

List size: 8
Value: h

I expected to see, Value: abcdefgh, and not just "h".

This will only convert a single char into a String ie you will have a String containing the single char, is this what you wanted?


Well no, I want all of the chars to be converted.
12 years ago
Hey Everyone, I have this strange LinkedList problem, not sure if the problem is within the list itself, or maybe the code, as I'm not very familiar with data structures yet. Before I get started, I understand that some of you might be wondering why I need to make all these data converting, from String to byte[] array, then from byte array to char, and eventually call Character.toString(). Well I will be sending some data over the network to servlet, the servlet will have to process this data and return to client. The data is stored in byte array, then I cycle through the array, convert every element into char (since you can cast byte to char), add every element into LinkedList, and extract what I need as a String by calling Character.toString(). Eventually what I need is, a few Strings send from the client. I understand, that there might be some better alternatives to this (and I'm sure there is), but still, why my code below doesn't work?.

What I'm trying to do here, is encode String parameter "name" into byte array, then cycle through the array and as I said earlier convert every element into char, then add these elements into the List. Then iterate over the list, and extract values that I need, by calling Character.toString(). So the output, is always the last character in the message, if type "abcdefgh" the output will be "h", if I type "Spain", the output is "n". So why is that?
I suspect that every node in my list just points to characters of the whole String. Even if it is that, why only "h" and not "a" ? (in case of "abcdefgh").

12 years ago
I just asked the same question couple of days ago (https://coderanch.com/t/595744/Tomcat/Tomcat-access), it all really depends on how you are connected to the internet. If you have a router, then check router settings and enable port forwarding to 8080. I had to add "service" on specific pc (with specific IP address, port, etc), but it all depends on what router vendor and software you are using. Otherwise, simply enable port forwarding to 8080 in your firewall settings, or use iptables (although, I'm not sure how to use that one).
12 years ago