toString本意是:返回该对象的字符串
- 没有重写toString()方法。
public void execQuety(String sql) throws SQLException {
System.out.println(sql);
System.out.println(connection);
PreparedStatement prep = connection.prepareStatement(sql);
System.out.println(prep);
ResultSet rs = prep.executeQuery();
while (rs.next()) {
Student student = new Student(rs.getString(1), rs.getString(2), rs.getString(3), rs.getInt(4));
System.out.println(student);
}
class Student {
String sno;
String name;
String sex;
Integer high;
public Student(String sno, String name, String sex, Integer high) {
super();
this.sno = sno;
this.name = name;
this.sex = sex;
this.high = high;
}
// @Override
// public String toString() {
// return "Student [sno=" + sno + ", name=" + name + ", sex=" + sex + ", high=" + high + "]";
// }
//
}
结果是
select * from student
oracle.jdbc.driver.T4CConnection@2d38eb89
oracle.jdbc.driver.OraclePreparedStatementWrapper@2344fc66
pro_test.a15_jdbc.StudyJDBC
S
t
u
d
e
n
t
@
5034
c
75
a
p
r
o
t
e
s
t
.
a
1
5
j
d
b
c
.
S
t
u
d
y
J
D
B
C
Student@5034c75a pro_test.a15_jdbc.StudyJDBC
Student@5034c75aprotest.a15jdbc.StudyJDBCStudent@396a51ab
pro_test.a15_jdbc.StudyJDBC
S
t
u
d
e
n
t
@
51081592
p
r
o
t
e
s
t
.
a
1
5
j
d
b
c
.
S
t
u
d
y
J
D
B
C
Student@51081592 pro_test.a15_jdbc.StudyJDBC
Student@51081592protest.a15jdbc.StudyJDBCStudent@7f9a81e8
pro_test.a15_jdbc.StudyJDBC$Student@9629756
2.重写toString()方法。
class Student {
String sno;
String name;
String sex;
Integer high;
public Student(String sno, String name, String sex, Integer high) {
super();
this.sno = sno;
this.name = name;
this.sex = sex;
this.high = high;
}
@Override
public String toString() {
return "Student [sno=" + sno + ", name=" + name + ", sex=" + sex + ", high=" + high + "]";
}
}
结果是
select * from student
oracle.jdbc.driver.T4CConnection@2d38eb89
oracle.jdbc.driver.OraclePreparedStatementWrapper@2344fc66
Student [sno=09010101, name=张三, sex=男 , high=172]
Student [sno=09010102, name=李斯, sex=男 , high=168]
Student [sno=09010201, name=王二, sex=男 , high=175]
Student [sno=09020101, name=赵武, sex=男 , high=185]
Student [sno=09020103, name=陈启, sex=男 , high=180]