一聚教程网:一个值得你收藏的教程网站

热门教程

jsp调用java类并查询sql数据库

时间:2022-11-14 23:25:44 编辑:袖梨 来源:一聚教程网

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.111com.net: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="上一页";
if(page 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;
}
}
%>

<%
String sql="select * from student";
ResultSet rs=db.query(sql);
while(rs.next()){
out.println(rs.getString(10);
}

热门栏目