Click here to Skip to main content
15,920,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello
i was haired in company they put me in training and i make simple app using JDBC to be familiar with how connecting java to mysql no my application is contain 5 class's student,course,teacher,section,main each one of this class's have setter and getter method expect main i should insert data to student table using student object and so on for all tables and objects now here is the code and ready to hear your opinions if there unlogical problems
Java
package com.query;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {
	public Teacher teacher;
	public Course course;
	public Section section;
	public Student student;
	public static Connection connecting;
	public static Statement st;
	public static ResultSet rs;

	public static void main(String[] args) throws Throwable {
		Class.forName("com.mysql.jdbc.Driver");
		connecting = DriverManager.getConnection(
				"jdbc:mysql://localhost/student", "root", "4896558");
		st = connecting.createStatement();
//		st.executeUpdate("insert into student(student_name,gender) values('khalel','male')");
		rs = st.executeQuery("Select * from student ");
		rs = st.executeQuery("Select * from student where idStudent=1");
		Student s1 = new Student();
		while (rs.next()) {
			s1.setId(rs.getInt(1));
			s1.setName(rs.getString(2));
			s1.setGender(rs.getString(3));
			System.out.println(s1.getId() + " " + s1.getName() + " "
					+ s1.getGender());
		}

		Course c1 = new Course();
//		st.executeUpdate("insert into course(course_name)  values('OOP advance')");
		rs = st.executeQuery("select * from course where idCourse=1");
		while (rs.next()) {
			c1.setId(rs.getInt(1));
			c1.setName(rs.getString(2));
			System.out.println(c1.getId() + " " + c1.getName());

		}
		Section s2 = new Section();
//		st.executeUpdate("insert into section1(name,teacherId,courseId) values('History',1,1)");
		rs = st.executeQuery("Select * from section1 where idsection=1 ");
		while (rs.next()) {
			s2.setId(rs.getInt(1));
			s2.setName(rs.getString(2));
			System.out.println(s2.getId() + " " + s2.getName());

		}
		Teacher t1 = new Teacher();
//		st.executeUpdate("insert into teacher (teacher_Name)values ('Amjad')");
		rs = st.executeQuery("Select * from teacher where idteacher=1 ");
		while (rs.next()) {
			t1.setId(rs.getInt(1));
			t1.setName(rs.getString(2));
			System.out.println(t1.getId() + " " + t1.getName());
		}
	}
}
Posted

The way to answer this question is for you to spend time testing it. You know what the program is supposed to do so you need to write test cases (and test data) for each of those situations. Then run through your tests a few times and you should soon see what is right and what is wrong about it. You will also learn far more than if someone tells you you have a logic error on line 23.
 
Share this answer
 
Comments
hamzah1 1-Dec-11 5:54am    
I make a test case's and done right but my trainer always come to me and start changing in my code and telling me be more logical when your start programming so , i need to learn how to make sure that my code is optimized to the last and make no one give a comments about the way my code written so do you have any idea how to make sure that my code is optimized enough ?
Richard MacCutchan 1-Dec-11 6:08am    
You are asking the wrong people. Obviously your trainer has some ideas about how your program should be structured so you should be asking his (or her) advice. Maybe it is something that you were given in the course that you missed.
Add some exception handling.

You can not depend on getting all values - what happens if a value is not valid (e.g. a number is out of integer range or not even a number, a String value is not set)?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900