java在jdbc中toString()的用法

toString本意是:返回该对象的字符串

  1. 没有重写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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值