急求一份实现数据库查询功能的java源代码,要求是能运行
1个回答
展开全部
// 本类只用于数据库连接及关闭操作
package com.ultrapower.project.nmr.shxdownload.sqlBean;
import java.sql.* ;
public class downloadSqlBean
{
// 属性
// 定义数据库操作的常量、对象
// 数据库驱动程序
private final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
// 数据库连接地址
private final String DBURL = "jdbc:oracle:thin:@192.168.99.78:1521:ultranms" ;
// 数据库用户名
private final String DBUSER = "ultranms" ;
// 数据库连接密码
private final String DBPASSWORD = "ultranms" ;
/旁森/ 声明一个数据库连接对象
private Connection conn = null ;
// 在构造方法之中连接数据库芹段
public downloadSqlBean()
{
try
{
// 加载驱动程序
Class.forName(DBDRIVER) ;
// 连接数据库
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
conn.commit();
}
catch (Exception e)
{
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(e) ;
}
}
// 返回一个数据库连接
public Connection getConnection()
{
/// 返回连接对象
return this.conn ;
}
// 关闭数据库连接
public void close()
{
try
{
this.conn.close() ;
}
catch (Exception e)
{
}
}
};
public class putDbUtils {
// 查询方法
public ResultSet putDbUtils(String sql) {
PreparedStatement pstm = null;
ResultSet rs = null;
downloadSqlBean bean = new downloadSqlBean();
try {
pstm = bean.getConnection().prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
rs = pstm.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
//action,得到resultset结果集.以后存储到map,之后set到list里.返回给jsp.在jsp遍历list获得到值就可以了
//SQL部分我用的是拼串.仅供参考.正好是嫌启誉我正在写的项目. 就直接粘上了
public List getAllDownloadFile(int stapage, int courpage) {
ResultSet rs = null;
String sql = null;
List list = new ArrayList();
int startpage = (stapage - 1) * courpage + 1;
int endpage = (stapage - 1) * courpage + courpage;
String sqlhead = "select * from (select A.*,rownum rn from(select filename,title,downloadway,ftpip,fileurl from nmr_download_file where 1=1";
String sqlpage = ") A where rownum <=" + endpage + ")where rn>="
+ startpage;
sql = sqlhead + sqlpage;
putDbUtils db = new putDbUtils();
rs = db.putDbUtils(sql);
try {
while (rs.next()) {
int conter = rs.getRow();
HashMap map = new HashMap();
map.put("filename", rs.getString("ftpname"));
map.put("title", rs.getString("ftpip"));
map.put("downloadway", rs.getString("ftpurl"));
map.put("ftpip", rs.getString("ftpuser"));
map.put("fileurl", rs.getString("ftppass"));
map.put("conter", conter);
list.add(map);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
package com.ultrapower.project.nmr.shxdownload.sqlBean;
import java.sql.* ;
public class downloadSqlBean
{
// 属性
// 定义数据库操作的常量、对象
// 数据库驱动程序
private final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
// 数据库连接地址
private final String DBURL = "jdbc:oracle:thin:@192.168.99.78:1521:ultranms" ;
// 数据库用户名
private final String DBUSER = "ultranms" ;
// 数据库连接密码
private final String DBPASSWORD = "ultranms" ;
/旁森/ 声明一个数据库连接对象
private Connection conn = null ;
// 在构造方法之中连接数据库芹段
public downloadSqlBean()
{
try
{
// 加载驱动程序
Class.forName(DBDRIVER) ;
// 连接数据库
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
conn.commit();
}
catch (Exception e)
{
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(e) ;
}
}
// 返回一个数据库连接
public Connection getConnection()
{
/// 返回连接对象
return this.conn ;
}
// 关闭数据库连接
public void close()
{
try
{
this.conn.close() ;
}
catch (Exception e)
{
}
}
};
public class putDbUtils {
// 查询方法
public ResultSet putDbUtils(String sql) {
PreparedStatement pstm = null;
ResultSet rs = null;
downloadSqlBean bean = new downloadSqlBean();
try {
pstm = bean.getConnection().prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
rs = pstm.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
//action,得到resultset结果集.以后存储到map,之后set到list里.返回给jsp.在jsp遍历list获得到值就可以了
//SQL部分我用的是拼串.仅供参考.正好是嫌启誉我正在写的项目. 就直接粘上了
public List getAllDownloadFile(int stapage, int courpage) {
ResultSet rs = null;
String sql = null;
List list = new ArrayList();
int startpage = (stapage - 1) * courpage + 1;
int endpage = (stapage - 1) * courpage + courpage;
String sqlhead = "select * from (select A.*,rownum rn from(select filename,title,downloadway,ftpip,fileurl from nmr_download_file where 1=1";
String sqlpage = ") A where rownum <=" + endpage + ")where rn>="
+ startpage;
sql = sqlhead + sqlpage;
putDbUtils db = new putDbUtils();
rs = db.putDbUtils(sql);
try {
while (rs.next()) {
int conter = rs.getRow();
HashMap map = new HashMap();
map.put("filename", rs.getString("ftpname"));
map.put("title", rs.getString("ftpip"));
map.put("downloadway", rs.getString("ftpurl"));
map.put("ftpip", rs.getString("ftpuser"));
map.put("fileurl", rs.getString("ftppass"));
map.put("conter", conter);
list.add(map);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
追问
你的怎么运行不了啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询