如何通过java将sql数据库的内容显示再列表中
1个回答
展开全部
你的数据库已经联上了,我就不写连接数据库的代码了,连接数据库的代码你可以写意个工具类,然后写成静态的方法,想要连接直接调用就好了!
下面给你看下操作数据库的代码
public
List
find()
{
Connection
conn
=
null;
PreparedStatement
ps
=
null;
ResultSet
rs
=
null;
List
list
=
new
ArrayList();
conn
=
JdbcUtil.getConnection();
try
{
conn.setAutoCommit(false);
String
sql
=
"select
*
from
student";
ps
=
conn.prepareStatement(sql);
rs
=
ps.executeQuery();
while(rs.next()){
Student
student
=
new
Student();
student.setId(rs.getInt(1));
student.setName(rs.getString(2));
student.setAddress(rs.getString(3));
list.add(student);
}
}
catch
(SQLException
e)
{
//
TODO
Auto-generated
catch
block
e.printStackTrace();
}finally{
JdbcUtil.close(conn,
ps,
rs);
}
return
list;
}
你可知直接直接把这几项用system打印在控制台上就好student.setId(rs.getInt(1));
student.setName(rs.getString(2));
student.setAddress(rs.getString
下面给你看下操作数据库的代码
public
List
find()
{
Connection
conn
=
null;
PreparedStatement
ps
=
null;
ResultSet
rs
=
null;
List
list
=
new
ArrayList();
conn
=
JdbcUtil.getConnection();
try
{
conn.setAutoCommit(false);
String
sql
=
"select
*
from
student";
ps
=
conn.prepareStatement(sql);
rs
=
ps.executeQuery();
while(rs.next()){
Student
student
=
new
Student();
student.setId(rs.getInt(1));
student.setName(rs.getString(2));
student.setAddress(rs.getString(3));
list.add(student);
}
}
catch
(SQLException
e)
{
//
TODO
Auto-generated
catch
block
e.printStackTrace();
}finally{
JdbcUtil.close(conn,
ps,
rs);
}
return
list;
}
你可知直接直接把这几项用system打印在控制台上就好student.setId(rs.getInt(1));
student.setName(rs.getString(2));
student.setAddress(rs.getString
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询