从10评估我的代码 [英] Evaluate my code from 10

查看:42
本文介绍了从10评估我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我在公司任职,他们让我接受了培训,我使用JDBC制作了一个简单的应用程序,以熟悉如何将Java连接到mysql否我的应用程序包含5个班级的学生,课程,老师,部分,班级中的每个班级的有setter和getter方法,期望主要是我应该使用Student对象将数据插入到Student表中,对于所有表和对象,依此类推,现在这里是代码,如果有不合逻辑的问题,您可以随时听取您的意见

  package  com.query;

导入 java.sql.Connection;
导入 java.sql.DriverManager;
导入 java.sql.ResultSet;
导入 java.sql.Statement;

公共  Main {
公共老师;
公共课程;
 public 板块部分;
 public 学生;
公共 静态连接正在连接;
 public  静态语句st;
公共 静态 ResultSet rs;

公共 静态  void  main(字符串 []参数)抛出可抛出{
Class.forName(" );
连接= DriverManager.getConnection(
"  " " );
st =连接.createStatement();
//  st.executeUpdate(插入学生(student_name,gender)值('khalel','male')" ); 
rs = st.executeQuery(" 中选择*);
rs = st.executeQuery(" );
学生s1 =  Student();
同时(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());
}

课程c1 =  Course();
//  st.executeUpdate(插入课程(course_name)值('OOP advance')"); 
rs = st.executeQuery(" 的过程中选择*);
同时(rs.next()){
c1.setId(rs.getInt( 1 ));
c1.setName(rs.getString( 2 ));
System.out.println(c1.getId()+ "  + c1.getName());

}
s2部分=  Section();
//  st.executeUpdate(插入到section1(name,teacherId,courseId)values('History',1,1 )); 
rs = st.executeQuery(" ));
同时(rs.next()){
s2.setId(rs.getInt( 1 ));
s2.setName(rs.getString( 2 ));
System.out.println(s2.getId()+ "  + s2.getName());

}
教师t1 =  Teacher();
//  st.executeUpdate(插入教师(teacher_Name)值('Amjad')"); 
rs = st.executeQuery(" );
同时(rs.next()){
t1.setId(rs.getInt( 1 ));
t1.setName(rs.getString( 2 ));
System.out.println(t1.getId()+ "  + t1.getName());
}
}
} 

解决方案

回答这个问题的方法是让您花费时间测试它.您知道程序应该执行的操作,因此您需要针对每种情况编写测试用例(和测试数据).然后运行几次测试,您应该很快就知道对与错.与其他人在第23行告诉您逻辑错误相比,您还将学到更多.


添加一些异常处理.

您不能依赖于获取所有值-如果值无效(例如,数字超出整数范围或什至没有数字,则未设置字符串值)会发生什么?


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

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());
		}
	}
}

解决方案

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.


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)?


这篇关于从10评估我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆