|
Member 13276968 wrote: String ans = null;
Should be empty string not null
Also noting that the code you have is not printing the result.
|
|
|
|
|
Dear experts,
I understand now that I have to get Generated Key in order to obtain my auto-incremental Id from a table.
However, after getting the generated key and set it to the model. I am stuck as to how to give it to my third table.
public void insertTutor(tutor m) throws MyDataException {
openConnection();
try {
connection.setAutoCommit(false);
} catch (SQLException e1) {
e1.printStackTrace();
}
String qry = INSERT_QRY;
try (
PreparedStatement ps = connection.prepareStatement(qry)) { ;
ps.setString(1, m.getName());
ps.setString(2, m.getNRIC());
ps.executeUpdate();
ResultSet tableKeys = ps.getGeneratedKeys();
tableKeys.next();
int tutor_id = tableKeys.getInt(1);
m.setTutor_id(tutor_id);
System.out.println(tutor_id);
connection.commit();
} catch (SQLException e) {
e.printStackTrace();
....
}}}}
And I do the same for my 2nd table.
My third table I need to make use of the Ids from the first and 2nd table.
So, do I create an insert like this ?
String qry = "INSERT INTO project.tutor_subject" + "(tutor_id, sub_id) values"
+ "(?,?)";
And how do I go about doing the third DAOImpl and the controller to get the Ids to go into the third table ?
I have read that JPA and using MyBatis is better for many to many table insertion.
Should I do it via JPA and MyBatis ?
|
|
|
|
|
karengsh,
It looks like you have most of your framework built out already, but JPA or MyBatis could also be used if you wish.
The advantage of using JPA is it handles almost all of the boilerplate database CRUD operations, but there is a learning curve to using it.
If you wish to continue on your current path, don't worry about a DAO for the tutor_subject join table, just put the logic in a manager class for the tutor entity.
Given the pattern I see in your code, you would simply use the getId() methods on the objects passed to your insertTutor and insertSubject methods
tutor tmpTutor = new tutor("Fred Flintstone","NRIC value");
subject tmpSubject = new subject("Java");
boolean success = false;
try {
insertTutor(tmpTutor);
insertSubject(tmpSubject);
success = true;
}
catch (MyDataException mde){
}
if (success){
try {
associateTutorToSubject(tmpTutor.getId(), tmpSubject.getId());
}
catch (MyDataException mde2){
}
}
|
|
|
|
|
Hi Mike,
My problem is how do I make the tmpTutor.getId() and tmpSubject.getId() mehtod static so that I could implement in my controller ?
Cos right now, I can only insert the tutorDAOImpl when I do the preparedStatement for tutor.
When I want to do the associateTutorToSubject, I can't do it the way like yo said cos tutorDAOImpl and SubjectDAOImpl are both separate classes. And so there is no way for the controller to read the getId from the tutorDAOImpl etc.
|
|
|
|
|
when i select jcombobox the database should retrieve in Jtable
|
|
|
|
|
Yes, it should. And if it doesn't it's a very naughty boy.
|
|
|
|
|
|
I try to bring a sense of perspective sometimes.
|
|
|
|
|
Hi! I am learning servlet, can someone kind enough to explain where to place the username, password, address, and port to be used in connection to a MySQL database in WAMP instead of placing it in the code? So that if the address of MySql or server is changed I will not have to change the code. Take the succeding as an example, "jdbc:mysql://localhost:3306/sonoo","root","root"). I am thinking if they are written in the code and they will be changed like the password for mysql will be changed, I will have to change the code. Thus, where will I put the localhost, 3306, sonoo, root, and root so that I will not change the code everytime the address and password, etc. will change. I am using netbeans and apache tomcat. Thank you so much.
|
|
|
|
|
|
How can I change tempo of a jetfile in android?
|
|
|
|
|
|
public class Try_Array
{
public void cool(int[] a)
{
Second_Arra(a);
First_Array(a);
}
private void First_Array(int[] a )
{
for (int i = 0; i < a.length; i++)
a[i] = 10;
}
private void Second_Arra(int[] a )
{
for (int i = 0; i < a.length; i++)
a[i] = 100;
}
}
public class ArrayDemo
{
public static void main(String[] args){
int[] a = {99, 5, 4, };
Try_Array=new Try_Array();
Obj.cool(a);
System.out.println("Array values after sorting:");
for (int i = 0; i < a.length; i++){
System.out.print(a[i] + " ");
}
System.out.println();
}
}
the Output :
Array values after sorting:
100 100 100
why Java Invoke First_Array(a) not Second_Arra(a); I got confuse !!!!! did java chose random method or what !
|
|
|
|
|
I cannot see how you can get any results, since that code will not even compile.
|
|
|
|
|
it's work fine the output :
Array values after sorting:
10 10 10
so my question how java invoke First_Array(a); not Second_Arra(a) ,
|
|
|
|
|
I copied the code from your question and it definitely does not compile.
|
|
|
|
|
public class ArrayDemo
{
public static void main(String[] args){
int[] a = {99, 5, 4, };
Try_Array Obj =new Try_Array();
Obj.cool(a);
System.out.println("Array values after sorting:");
for (int i = 0; i < a.length; i++){
System.out.print(a[i] + " ");
}
System.out.println();
}
}
public class Try_Array
{
public void cool(int[] a)
{
Second_Arra(a);
First_Array(a);
}
private void First_Array(int[] a )
{
for (int i = 0; i < a.length; i++)
a[i] = 10;
}
private void Second_Arra(int[] a )
{
for (int i = 0; i < a.length; i++)
a[i] = 100;
}
}
Ok Sorry my Mistake , i fix it now , why and how java invoke First_Array(a); not Second_Arra(a); is that random , I got confuse
|
|
|
|
|
there is nothing random in this, it calls the methods in the order you have coded. So First_Array is called last, and all values are set to 10, there is no sorting done.
|
|
|
|
|
thanks so you saying , java did first calling for Second_Arra(a); then did call First_Array(a);
so Java brought ma final value to the main method .
ok that make sense now .
but still not logic for me how java process the code , for example :
I didn't create instance variable for the class Try_Array.
i don't declare variable like int a[]; and i don't create set() and get() . i don't even do return method !
how for loop (int i = 0; i < a.length; i++) on the main method knows that a[] is got handled with Try_Array class and that array got full with new value !
I just no longer able to trace how java process this I really lost and got crazy to fail understand simple thing
when I build the code I build return method that brought final value I usually build private instance variable for the class . but I got confuse when I saw this code on book not adhere to what I learn
|
|
|
|
|
The code does exactly what you tell it:
- You create the array
a with some values - You create the Obj object
- You call Obj.cool passing in the array
- Obj.cool calls Second_Arra which sets all the values to 100
- Obj.cool next calls First_Array which sets all the values to 10
- You print the contents of the array
|
|
|
|
|
ok thank you so much I would like to clear my point this time how about passing variable
public class ArrayDemo
{
public static void main(String[] args){
int a = 5;
Try_Array Obj =new Try_Array();
Obj.cool(a);
System.out.println("A value After Calling:");
System.out.print(a );
}
}
public class Try_Array
{
public void cool( int a)
{
First_Array(a);
Second_Arra(a);
}
private void First_Array(int a )
{
a=4;
}
private void Second_Arra(int a )
{
a=6;
}
}
the out put is :
A value After Calling:
5
it' should be 6 ; but seam java ignore invocation with variable but java not behave like this with array
do you see my point when I pass array it got effect and back with different but when I send variable back nothing and it will not get effect or change the variable !
|
|
|
|
|
No, it should be 5. Since this is a simple variable, it is passed by value, so each called method gets a copy of the variable a. When they try to change it they only change the copy they have received, not the original variable. You should go to The Java™ Tutorials[^], especially section Passing Information to a Method or a Constructor[^] which explains it in detail.
|
|
|
|
|
Dear Richard your comment help me a lot thank you for guide me to the important topic and basic point that I really miss , you know what I spend 3 month reading with java when I reach recursion it was setback for me I shocked when book give example with mergesort, he build array on Fantasy way that I got really confuse and frustrated that i'm able to follow so I just back for reading previous chapters .
thank you so much I really appreciate your time.
|
|
|
|
|
Hello!
This is my first topic and I will search for other forums as well that might be more into ANN's. As the subjects indicates I have a huge interest in ANN's.
I would like to find people that are intereseted in ANN's and want to code or is coding already in Java and learn more about code implementation, testing and experimenting with ANN's.
I have built my own "library" and implemented it partially. It solves the XOR problem by using LMS algorithm for updating weights and now I am trying to implement the Backpropagation algorithm. Which is a pain in the ass becuase of the way I am storing stuff in my arrays. Meaning that calculating the backpropagation-deltas for the hiddenlayer is a bit of a challenge right now and that is where I am.
I have also creted classes that are objects such as Layer.java, Neuron.java and Weight.java. I will rewrite my code so that I use these classes as Layer[], Neuron[] and Weight[] instead of double[] and int[].
My bachelor topic had to do with topology mutating ANN's that use a simple genetic mutation of the weights to update the network and also the topology; a kind of placticity.
I am up for sharing this on GitHub and further developing it together with someone that also has a huge interest in this stuff! My goal is to reach to the point where a reward-based ANN, combined with this new code structure I am working on and with the code from my bachelor project. For example I would like to try to make ANN's cooperate with other ANN's. So that one ANN acts as visual input while another ANN acts as a generalization network trying to understand and categorize what is "seen".
So is anyone interested in helping and discussing this and coming with ideas and challenges? Or do you have any thoughts about this?
Also if you know any forum that is more suited for ANN then please suggest it! I would be very happy!
|
|
|
|
|
Member 13241109 wrote: I have also creted classes that are objects such as Layer.java, Neuron.java and Weight.java. I will rewrite my code so that I use these classes as Layer[], Neuron[] and Weight[] instead of double[] and int[]. Why weights and neurons? This just adds a whole bunch of pointless overhead, and no significant encapsulation. The only thing worth encapsulating is a whole layer, and only because you can have different types of layer (standard, conv, pooling, etc).
|
|
|
|