jsp教程调用java类并查询sql数据库教程
package bean;
import java.sql.*;
public class MyDb_Page{
private Statement stmt;
private Connection conn=null;
private ResultSet rs=null;
private String navi;
public void connection(String db_name,String user,String pwd) throws Exception{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:mysql教程://www.update8.com:3306/" + db_name;
conn=DriverManager.getConnection(url,user,pwd);
stmt=conn.createStatement();
}
public ResultSet query(String sql) throws Exception{
rs=stmt.executeQuery(sql);
return rs;
}
public ResultSet query(String query,int size,int page) throws Exception{
int page_count; //总页数
int result_count; //总记录数
int begin; //当前页第一条记录索引号
String sql=query;
int i=query.indexOf("from");
String sql2=query.substring(i);
rs=query("select count(*) " + sql2);
rs.next();
result_count=rs.getInt(1);
page_count=(result_count + size - 1)/size;
begin=(page-1)*size;
String s=" limit " + begin + "," +size;
sql=sql + s;
rs=query(sql);
//设置导航栏
String back="没有了";
String pre="没有了";
if(page>1) back="<a href=?page=" + String.valueOf(page-1) + ">上一页</a>";
if(page<page_count) pre="<a href=?page=" + String.valueOf(page+1) + ">下一页</a>";
navi=back + " " + pre;
return rs;
}
public String getNavi(){
return navi;
}
public int update(String sql) throws Exception{
int n=0;
n=stmt.executeUpdate(sql);
return n;
}
public boolean delete(String sql) throws Exception{
boolean flag=false;
flag=stmt.execute(sql);
return flag;
}
}
%>
<jsp:useBean id="www.update8.com" class="bean.MyDb_Page" scope="page"/>
<%
String sql="select * from student";
ResultSet rs=db.query(sql);
while(rs.next()){
out.println(rs.getString(10);
}
更多信息请查看IT技术专栏