jsp教程調(diào)用java類并查詢sql數(shù)據(jù)庫教程
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; //總頁數(shù)
int result_count; //總記錄數(shù)
int begin; //當(dāng)前頁第一條記錄索引號
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);
//設(shè)置導(dǎo)航欄
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技術(shù)專欄
2025國考·省考課程試聽報名