|
|
WHICH ERRORS?
Do you actually have errors while running the code - I mean this red text running in that little fancy window below your code - you know? Is it to much to ask for copying it in here?
Otherwise we can just tell you that we're also sometimes "facing errors"
regards
Torsten
I never finish anyth...
|
|
|
|
|
Once again you seem to be trying to build an application line by line without spending some time reading the documentation and getting a grasp of the fundamentals. Take a look here[^] at the Java API documentation and check the requirements of the features you are trying to use.
I must get a clever new signature for 2011.
|
|
|
|
|
You are not handling the exceptions these JDBC methods throw. You should take some time to learn the basics of JDBC programming. There are plenty of good books available, and lots of on-line tutorials if you cannot afford a book.
|
|
|
|
|
i have got it.
As previously i am using the properties file to get Property and i have this getProperty method as shown below:
public static String getProperties(String inputProperty) {
Properties props = new Properties();
try {
try {
props.load(Class.forName("......autoMessage").getResourceAsStream("autoMessage.properties"));
} catch (ClassNotFoundException ex) {
Logger.getLogger(autoMessage.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException ex) {
Logger.getLogger(autoMessage.class.getName()).log(Level.SEVERE, null, ex);
}
String output = props.getProperty(inputProperty);
return output;
}
Do i have to create a method in order to get the value from the database?
|
|
|
|
|
I'm afraid the best advice I can give you is to:
- Read up about Exception handling
- Read up about Properties
- Read up about JDBC and database programming
I get the feeling you are trying to teach yourself Java, which is admirable but you seem to be missing some basic grounding. A good book would help. Many people here would be happy to recommend one.
In your code above: you don't need to nest try blocks in the way you have done, you can have multiple catch statements for a single try. Also, it's probably inefficient to load the Properties every time you come to get a property, you can load them once up front.
I have no idea what your question about creating a method to get the value from the database means. Basically what you need to do is load the properties from the database instead of loading from a properties file.
|
|
|
|
|
yeah you are right i am learning java on my own and my only hope is in here. Hope you guys will understand if i asked something really simple. Yes what i want to do is load the properties from the database instead of loading from a properties file. I have 3 properties files in the project. I manage to change 2 of them and still have 1 not done yet.
I came across this problem for the third file. It is a .java file. My code as shown below:
public class emailNotification {
private static final String SMTP_HOST_NAME = getProperties("SMTP_HOST_NAME");
private static final int SMTP_HOST_PORT = Integer.parseInt(getProperties("SMTP_HOST_PORT"));
private static final String SMTP_AUTH_USER = getProperties("SMTP_AUTH_USER");
private static final String SMTP_AUTH_PWD = getProperties("SMTP_AUTH_PWD");
private emailNotification() {
}
I am unable to create a database connection in the public class even after i did the try and catch.
Any idea how shld i do this?
another question to ask:
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
The above code is to create a new property right? is it appropriate for me to use in my case whereby i will load the properties from the database instead of loading from a properties file.
modified on Thursday, January 27, 2011 1:39 AM
|
|
|
|
|
pancakeleh wrote: I am unable to create a database connection in the public class even after i did the try and catch.
What have you tried? If you read the tutorial on JDBC it shows you how to create a database connection. I am not a mind-reader, so I have no idea why you are having a problem unless you give us some more information.
pancakeleh wrote: The above code is to create a new property right? is it appropriate for me to use in my case whereby i will load the properties from the database instead of loading from a properties file.
Yes, this is pretty much it. In pseudo-code you would do something like:
- Connect to the database
- Get the properties from the database using a SELECT statement
- Iterate over the result set
- For each row in the result set, add key/value into the properties map
- Close database connection
That's roughly it.
|
|
|
|
|
I have made the same suggestion a number of times, even providing links to the tutorial pages, but he/she still keeps coming back with similar questions. I hope s/he's not getting paid for this.
I must get a clever new signature for 2011.
|
|
|
|
|
...you will at least need one properties file defining the database for your application.
You can set up the properties in a folder aside the source:
- Source "src"
- Resources "res"
- "images"
- "properties"
- "database"
- "application"
- ....
And it is possible to add encryption to them.
regards
Torsten
I never finish anyth...
|
|
|
|
|
hi guys sorry i've been away for a long time, recently i've been doing some basic java programming, i've read a few things about bytecode which is something similar to asm but more abstract, and my question is, can it be possible to program in bytecode, if it is is there a manual out there on the web to start off with cuz i've been googling about it and i can't find any, i think if it is possible to program using bytecode the program itself would be more neat and faster as well as it is when u program in assembly.
|
|
|
|
|
Bytecode is Java's intermediate language that gets translated to machine code by the Java run time system. I have a feeling that there is documentation available for the IL but it is proprietary to Oracle (Sun) and you have to pay to get hold of a copy. Try the Java web sires for more information.
I must get a clever new signature for 2011.
|
|
|
|
|
Richard MacCutchan wrote: I have a feeling that there is documentation available for the IL but it is proprietary to Oracle (Sun) and you have to pay to get hold of a copy.
No, it is available for free as part of the published Java VM Specification.
Richard MacCutchan wrote: Try the Java web sires for more information.
Java web sire: I like the sound of that. I think I will put it on my CV.
|
|
|
|
|
David Skelly wrote: Java web sire: I like the sound of that. I think I will put it on my CV.
The father of ...
I must get a clever new signature for 2011.
|
|
|
|
|
geniuspc wrote: i think if it is possible to program using bytecode the program itself would be more neat and faster as well as it is when u program in assembly.
That's certainly far from being true, otherwise why would there be any high-level languages like Java ? You tend to forget that designing a very big program in assembler becomes very quickly something totally unmaintainable and with probably a lot more bugs than if you would write in in C++ (for instance). Most of the time, the compiler is smart enough to generate efficient assembly code from your C++ code and the higher-level abstraction forces you to avoid certain "errors".
What you seem to forget is that a well-written C++ code will probably run faster and with less errors than a badly written assembler code. And the assembler language is not really well suited for writing large applications which might need to be refactored or adapted in the future.
To finish, I will simply refer to a quote from Donald Knuth: "Premature optimization is the root of all evil". Which is what you are trying to do in your case: assembler is faster than C++, so I will write my code in assembler (and you forget about the humain in the chain that would write more efficient code in C++ than in assembler).
|
|
|
|
|
Yes, it is possible. Yes, there is documentation available. Yes, there are editors available to let you do it.
But unless you have some very specific requirements you would have to be completely barmy to consider writing bytecode by hand. The fact that you can do something doesn't mean that you should.
|
|
|
|
|
geniuspc wrote: i think if it is possible to program using bytecode the program itself would be more neat and faster as well as it is when u program in assembly
That is wrong. Perhaps bytecode looks like assembly, smells like assembly, tastes like assembly, but it is still bytecode. Probably it would be less neat, and sure it would not be faster.
|
|
|
|
|
To reiterate what others have said, writing byte code rather than Java would be synonymous to writing machine code rather than C.
Consider the problem domain. If there is a genuine performance issue, then it could be addressed using a lower level language - byte code or even assembler / machine code - but more often than not a high level language - C/ C++ for examples - and a targeted compiler will be enough.
In my limited experience - a mere 25 years - the better abstraction the language provides the less problems there will be with the code. The majority of bugs are due to simple mistakes by the programmer, by reducing the possibility of these the quality of the code is improved.
Panic, Chaos, Destruction.
My work here is done.
or "Drink. Get drunk. Fall over." - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
|
|
|
|
|
Hi,
I'm new to using Runtime.getRuntime().exec on WindowsXP, JRE 6.
Solution should work on all Windows NT from 5 upwards and common UNIX.
I called it in the following way
process = Runtime.getRuntime().exec(cmdargs);
where
cmdargs[0] = "D:\Somepath\NmMutex"
cmdargs[1] = "lock"
Program NmMutex with argument lock runs until its process is stopped externally.
When
Runtime.getRuntime().exec(cmdargs);
is called, the calling thread remains suspended, until program NmMutex is killed.
Is that the normal behaviour of Runtime.exec? I thought the calling thread would return, even if the program it started is still running, so I can observe the prog by reading its stdout and stderr from my Java app. If not it would return there would be no process created and I could not observe the program.
Which way is best to solve that? Start prog in Background, like
process = Runtime.getRuntime().exec("start /B NmMutex lock");
resp.
process = Runtime.getRuntime().exec("& NmMutex lock");
?
Thank you
Werner
|
|
|
|
|
It looks like Runtime.exec() is blocking on the process it spawned (like the old Unix exec()?). I can think of two portable solutions:
1. start a new Thread to do the exec() and let it block.
2. use ProcessBuilder.start()[^] This article[^] discusses the use of ProcessBuilder and Runtime.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
It looks like that, although I'm much surprised, because this is not what Runtime.exec should do, when I read into the docs - ehm - assumed I understood them also.
1 might work, but you don't get the process handle, because calling thread is suspended before return from call
2 yes right, I think its worth trying because it seems to be more convenient
thank you
Werner
|
|
|
|
|
I couldn't see anywhere the blocking/non-blocking behaviour of exec() was specified.
I think I read somewhere that ProcessBuilder is the preferred technology since Java 5. Certainly looks a lot more usable, although I haven't used either in anger.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
Hi,
I tried the same with process builder
ProcessBuilder pbuilder = new ProcessBuilder(cmdlist);
process = pbuilder.start();
procmon = new ProcessMonitor(process);
where
cmdlist[0] = "D:/MyPath/NmMutex"
cmdlist[1] = "lock" (btw. this doesn't mean the main thread waits at lock, its try_lock)
The thread returns as soon as the process, which was called ended. I tested it with a loop whithin the external process. If it is finite, the thread returns. If it is infinite the thread doesn't return. I.e. exec and start are blocking.
The idea to call ProcessBuilder.start within a separate process is basically good, but I woudn't receive a process handle when start would not return. And without that, I would not be able to attach streams to the stdout and stderr. Hence the process might block as soon as the stdout and stderr buffers are filled up.
If I started a background shell and within that my process, the solution would be os dependend (not KISS, complicated, not that good).
Thanks
Werner
|
|
|
|
|
Hi,
the process started by ProcessBuilder
and by Runtime.exec - which is implemented via ProcessBuilder -
is started asynchronously when the java app is started from jar.
The effect of the frozen thread happened exclusively within the debbuger session.
I should have tried.
Sorry
and thanks again
Werner
|
|
|
|
|
// I need someone to help me fix this codes
// it's about CALCULATING in Java, thanks alot...
import java.util.Scanner;
public class Calculate
{
static Scanner in = new Scanner(System.in);
public static void main(String [] args)
{
String sign;
int num1;
int num2;
int num3;
int ans;
System.out.print("Enter your Calculations => \n");
sign = in.nextLine();
if(args.Length == 3)
{
num1 = Integer.parseInt(args[0]);
sign = (args [1].equal("1"));
num2 = Integer.parseInt(args[2]);
num3 = Integer.parseInt(args[3]);
ans = num1 + num2;
ans = num1 - num2;
ans = num1 / num2;
System.out.println("Answer = "+ ans);
}else if(args[1].equal("+")) {
ans = num1 + num2;
System.out.println("num1 "+ "num2 " = ans);
}
else if(args[2].equal("-")) {
ans = num1 - num2;
System.out.println("num1 "- "num2 "= ans);
}
else if(args[3].equal("/")) {
ans = num1 / num2;
System.out.println("num1 "/ "num2 "= ans);
}
else if("Do it again => Calculate arg1, arg2, arg3");
}
}
}
modified on Wednesday, January 19, 2011 9:58 AM
|
|
|
|