数据库简单查询输出怎么是对象地址
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库简单查询输出怎么是对象地址相关的知识,希望对你有一定的参考价值。
public class Data
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/first?useUnicode=true&characterEncoding=GBK";
String use = "root";
String password = "147185120";
//在数据库里面查询语句的方法
public Friend[] findUser()
List<Friend> li = new LinkedList<Friend>();
Friend f = new Friend();
Connection con = null;
Statement stm = null;
ResultSet re = null;
String findSQL = "select id,name,age from one";
try
Class.forName(driver);
con = DriverManager.getConnection(url, use, password);
stm = con.createStatement();
re = stm.executeQuery(findSQL);
while(re.next())
int index = 1;
f.setId(re.getInt(index++));
f.setName(re.getString(index++));
f.setAge(re.getInt(index++));
li.add(f);
catch(Exception e)
e.printStackTrace();
finally
if(con != null)
try
con.close();
catch(SQLException e1)
e1.printStackTrace();
Friend [] arr = li.toArray(new Friend[0]);
return arr;
public static void main(String [] args)
Data da = new Data();
//da.createUser();
System.out.println(Arrays.toString(da.findUser()));
最后输出的是[Friend@19c26f5, Friend@19c26f5, Friend@19c26f5, Friend@19c26f5]
我就郁闷了撒
对了,还1个Friend 类主要就是javaBEAN 对名字,ID和年龄的设置
这里就没有写出来了
现在问题变了,就是他们的值怎么是1样的?????
因为他们内存地址1样啊,,
但是我的表里面有4个ID的嘛
我要把那4个ID对应的全部输出来
public String toString()
return "ID: "id + ", name= " + name + ", Age= " + age;
你原来的结果,和数据库没有关系,因为已经读出数据了,现在数据在内存里,输出的那一串也是内存中的地址19c26f5,toString方法默认是输出 类名@地址
还有什么问题再说
加油!加油! 参考技术A 你打出来的是 地址 说明里面有8个数据吧
我没看你的代码 太长了啊
按你的意思 应该是查询完 返回一个collection``吧
Collection co=null;
Iterator it=co.iterator();
while(it.hasNext())
System.out.println( it.next() );
用个 Iterator 碟代就能输出了 参考技术B toString()方法。
php怎么实现输出全国的省市区?
php怎么输出全国的省市区地址?
或者利用行政区划代码
http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2013/index.html
求代码,只有思路的别回复了!
1 =>
array (
\'province_name\' => \'北京市\',
\'city\' =>
array (
1 =>
array (
\'city_name\' => \'市辖区\',
\'area\' =>
array (
1 => \'东城区\',
2 => \'西城区\',
3 => \'崇文区\',
4 => \'宣武区\',
5 => \'朝阳区\',
6 => \'丰台区\',
7 => \'石景山区\',
8 => \'海淀区\',
9 => \'门头沟区\',
10 => \'房山区\',
11 => \'通州区\',
12 => \'顺义区\',
13 => \'昌平区\',
14 => \'大兴区\',
15 => \'怀柔区\',
16 => \'平谷区\',
),
),
2 =>
array (
\'city_name\' => \'北京周边\',
\'area\' =>
array (
1 => \'密云县\',
2 => \'延庆县\',
),
),
),
),
2 =>
array (
\'province_name\' => \'天津市\',
\'city\' =>
array (
1 =>
array (
\'city_name\' => \'市辖区\',
\'area\' =>
array (
1 => \'和平区\',
2 => \'河东区\',
3 => \'河西区\',
4 => \'南开区\',
5 => \'河北区\',
6 => \'红桥区\',
7 => \'塘沽区\',
8 => \'汉沽区\',
9 => \'大港区\',
10 => \'东丽区\',
11 => \'西青区\',
12 => \'津南区\',
13 => \'北辰区\',
14 => \'武清区\',
15 => \'宝坻区\',
),
),
2 =>
array (
\'city_name\' => \'天津周边\',
\'area\' =>
array (
1 => \'宁河县\',
2 => \'静海县\',
3 => \'蓟 县\',
),
),
),
),
3 =>
array (
\'province_name\' => \'河北省\',
\'city\' =>
array (
1 =>
array (
\'city_name\' => \'石家庄市\',
\'area\' =>
array (
1 => \'长安区\',
2 => \'桥东区\',
3 => \'桥西区\',
4 => \'新华区\',
5 => \'井陉矿区\',
6 => \'裕华区\',
7 => \'井陉县\',
8 => \'正定县\',
9 => \'栾城县\',
10 => \'行唐县\',
11 => \'灵寿县\',
12 => \'高邑县\',
13 => \'深泽县\',
14 => \'赞皇县\',
15 => \'无极县\',
16 => \'平山县\',
17 => \'元氏县\',
18 => \'赵 县\',
19 => \'辛集市\',
20 => \'藁城市\',
21 => \'晋州市\',
22 => \'新乐市\',
23 => \'鹿泉市\',
),
),
2 =>
array (
\'city_name\' => \'唐山市\',
\'area\' =>
array (
1 => \'路南区\',
2 => \'路北区\',
3 => \'古冶区\',
4 => \'开平区\',
5 => \'丰南区\',
6 => \'丰润区\',
7 => \'滦 县\',
8 => \'滦南县\',
9 => \'乐亭县\',
10 => \'迁西县\',
11 => \'玉田县\',
12 => \'唐海县\',
13 => \'遵化市\',
14 => \'迁安市\',
),
),
3 =>
array (
\'city_name\' => \'秦皇岛市\',
\'area\' =>
array (
1 => \'海港区\',
2 => \'山海关区\',
3 => \'北戴河区\',
4 => \'青龙满族自治县\',
5 => \'昌黎县\',
6 => \'抚宁县\',
7 => \'卢龙县\',
),
),
4 =>
array (
\'city_name\' => \'邯郸市\',
\'area\' =>
array (
1 => \'邯山区\',
2 => \'丛台区\',
3 => \'复兴区\',
4 => \'峰峰矿区\',
5 => \'邯郸县\',
6 => \'临漳县\',
7 => \'成安县\',
8 => \'大名县\',
9 => \'涉 县\',
10 => \'磁 县\',
11 => \'肥乡县\',
12 => \'永年县\',
13 => \'邱 县\',
14 => \'鸡泽县\',
15 => \'广平县\',
16 => \'馆陶县\',
17 => \'魏 县\',
18 => \'曲周县\',
19 => \'武安市\',
),
),
5 =>
array (
\'city_name\' => \'邢台市\',
\'area\' =>
array (
1 => \'桥东区\',
2 => \'桥西区\',
3 => \'邢台县\',
4 => \'临城县\', 参考技术A <?php
$con = mysql_connect('localhost','root','');
if($con)
mysql_query('use city');
mysql_query('set names utf8');
else
echo "数据连接失败";
function myquery ($sql)
$res = mysql_query($sql);
return $res ? $res : false;
function getArray($sql)
$res = myquery($sql);
//var_dump($res);
if($res)
while($row = mysql_fetch_assoc($res))
$rows[] = $row;
return $rows ? $rows : null;
function getRow($sql)
$res = getArray($sql);
return $res[0] ? $res[0] : null;
function getOne($sql)
$res = myquery($sql);
if($res)
$num = mysql_num_rows($res);
return $num ? $num :0;
$sql = "select p.pid,p.pname,c.cid,c.cname,d.did,d.dname ";
$sql .= " from s_province as p left join s_city as c on p.pid = c.pid ";
$sql .= " left join s_district as d on d.cid = c.cid ";
$res = getArray($sql);
foreach($res as $val)
$list[$val['pid']] [] = $val;
var_dump($list);
应该可以输出你想要的内容。至于页面显示用html和css 和js就行了。这里就不介绍了
差一点就完美了,怎么去掉多余的代码啊?
追答没有多余的代码,都是封装的函数,你自己看着去掉就行了
本回答被提问者和网友采纳 参考技术B 多级联动…以上是关于数据库简单查询输出怎么是对象地址的主要内容,如果未能解决你的问题,请参考以下文章
jstl标签和el表达式如何输出list中的object对象,object对象是数据库多表查询的结果,不是一个bean的字段