谁有JAVA按条件查询数据库内容并显示出来的源代码 能给我看下吗???谢谢啦 本人新手~
展开全部
1.用jdbc连接数据库
public class DBConn {
private Connection con;
public Connection DBConn() {
//连接数据库的属性
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
//获得数据库的真实路径
String dburl = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""));
dburl = dburl + "lyb.mdb";//得到文件的URL: 'file:/C:\...'
dburl = dburl.substring(dburl.indexOf("/")+1);//去掉'file:/'
//dburl.replaceAll("%20"," ");
System.out.println("dburl :"+dburl);//测试用,运行的时候显示绝对路径
dburl="E:/lyb/WebRoot/WEB-INF/classes/lyb.mdb";
String accessUrl="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+dburl;
try{
Class.forName(driver);
con = DriverManager.getConnection(accessUrl);
}catch(Exception e){
e.printStackTrace();
}
return con;
}
}
2.比如查询数据库中的record表,record表中存放的是实体MessageVO.
public Collection getMessage() throws Exception{
//SQL语句,从record里面获得所有数据
String getLy="select count(*) from record";
String getLy1="select * from record order by id desc";
Collection ret=new ArrayList();
try{
if(con==null) System.out.println("con=null");
Statement stm=con.createStatement();
ResultSet res=stm.executeQuery(getLy);
int msg_count=0;
if(res.next()){
msg_count=res.getInt(1);
res.close();
}
if(msg_count>0){
res=stm.executeQuery(getLy1);
while(res.next()){
int id=res.getInt("id");
String title=res.getString("title");
String name=res.getString("name");
String email=res.getString("email");
String content=res.getString("content");
java.sql.Timestamp ts=res.getTimestamp("ly_date");
java.sql.Date date=new java.sql.Date(ts.getTime());
//System.out.println(date);
MessageVO message=new MessageVO();
message.setId(id);
message.setTitle(title);
message.setName(name);
message.setEmail(email);
message.setContent(content);
message.setDate(date);
ret.add(message);
}
res.close();
stm.close();
}
con.close();
}catch(Exception e){
e.printStackTrace();
throw e;
}
return ret;
}
public class DBConn {
private Connection con;
public Connection DBConn() {
//连接数据库的属性
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
//获得数据库的真实路径
String dburl = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""));
dburl = dburl + "lyb.mdb";//得到文件的URL: 'file:/C:\...'
dburl = dburl.substring(dburl.indexOf("/")+1);//去掉'file:/'
//dburl.replaceAll("%20"," ");
System.out.println("dburl :"+dburl);//测试用,运行的时候显示绝对路径
dburl="E:/lyb/WebRoot/WEB-INF/classes/lyb.mdb";
String accessUrl="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+dburl;
try{
Class.forName(driver);
con = DriverManager.getConnection(accessUrl);
}catch(Exception e){
e.printStackTrace();
}
return con;
}
}
2.比如查询数据库中的record表,record表中存放的是实体MessageVO.
public Collection getMessage() throws Exception{
//SQL语句,从record里面获得所有数据
String getLy="select count(*) from record";
String getLy1="select * from record order by id desc";
Collection ret=new ArrayList();
try{
if(con==null) System.out.println("con=null");
Statement stm=con.createStatement();
ResultSet res=stm.executeQuery(getLy);
int msg_count=0;
if(res.next()){
msg_count=res.getInt(1);
res.close();
}
if(msg_count>0){
res=stm.executeQuery(getLy1);
while(res.next()){
int id=res.getInt("id");
String title=res.getString("title");
String name=res.getString("name");
String email=res.getString("email");
String content=res.getString("content");
java.sql.Timestamp ts=res.getTimestamp("ly_date");
java.sql.Date date=new java.sql.Date(ts.getTime());
//System.out.println(date);
MessageVO message=new MessageVO();
message.setId(id);
message.setTitle(title);
message.setName(name);
message.setEmail(email);
message.setContent(content);
message.setDate(date);
ret.add(message);
}
res.close();
stm.close();
}
con.close();
}catch(Exception e){
e.printStackTrace();
throw e;
}
return ret;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询