怎样用JSP语言查询数据库中的数据,并可以修改
就是将数据库中的数据,当点击【查询个人信息时】可以把数据库中的数据调出来,以表格的形式,且可以修改表格中的内容。...
就是将数据库中的数据,当点击【查询个人信息时】可以把数据库中的数据调出来,以表格的形式,且可以修改表格中的内容。
展开
3个回答
展开全部
查询什么数据库?
testsqlserver.jsp如下
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//pubs为你的数据库的
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
testoracle.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为你的数据库的SID
String user="scott";
String password="tiger";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
testdb2.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample";
//sample为你的数据库名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
testmysql.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//testDB为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
有了 这些代码,不代表你就能连接数据库了,你还得有相应java连接各个数据库的.jar包加入到你的工程当中!
testsqlserver.jsp如下
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//pubs为你的数据库的
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
testoracle.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为你的数据库的SID
String user="scott";
String password="tiger";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
testdb2.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample";
//sample为你的数据库名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
testmysql.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//testDB为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
有了 这些代码,不代表你就能连接数据库了,你还得有相应java连接各个数据库的.jar包加入到你的工程当中!
展开全部
实现方法很多,
1,可以直接在JSP里面写;
2,也可以采用MVC模式,用Servlet+JSP结合 ,因为JSP实际上就是一个Servlet;
3,也可以用JSP标签+JavaBean实现;
给你java bean的示例
package lDB;
import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletContext;
import javax.servlet.jsp.JspWriter;
public class SqlHelper
{
static private Connection conn; //连接对象
static private String dbpath=""; //路径
static private String defaultdbpath="db/maindb.mdb"; //默认路径
static private String dbdriver= "sun.jdbc.odbc.JdbcOdbcDriver";//保存Access驱动程序字符串
static private String connstr="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="; //保存Access连接字符串
static private JspWriter out; //用于错误输出
static private int DbType=0;
//初始化
static public void init(String dbp,JspWriter o,ServletContext app) throws IOException
{
out=o;
dbpath=app.getRealPath(dbp);
getConn();
}
//初始化
static public void init(JspWriter o,ServletContext app) throws IOException
{
dbpath=defaultdbpath;
init(dbpath,o,app);
}
//建立连接
static public Connection getConn() throws IOException{
String cstr="";
try{
if(conn!=null) return conn;
cstr=connstr+dbpath;
Class.forName(dbdriver);
conn = DriverManager.getConnection(cstr);
}catch(Exception e){
conn=null;
serr("SqlHelper.getConn:"+e.getMessage()+";连接字串:"+cstr);
}
return conn;
}
//得到RS
static public ResultSet getRs(String sql) throws IOException{
return getRs(sql,ResultSet.CONCUR_READ_ONLY);
}
static public ResultSet getRs(String sql,int ConcurType) throws IOException{
if(conn == null)
{
serr("SqlHelper.getRs:数据库还没有打开,不能得到rs");
return null;
}
ResultSet rs=null;
try{
Statement stmt=conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,ConcurType);
rs=stmt.executeQuery(sql);
}
catch(SQLException e){
serr(e.getMessage());
}
return rs;
}
static public int Insert(String tbl,String[] flds,Object[] values) throws IOException, SQLException
{
String sql="select * from "+tbl+" where 1=2;";
ResultSet rs=getRs(sql,ResultSet.CONCUR_UPDATABLE);
rs.moveToInsertRow();
for(int i=0;i<flds.length;i++){
rs.updateObject(flds[i], values[i]);
}
rs.insertRow();
return 1;
}
static public int Update(String tbl,String[] flds,Object[] values) throws IOException, SQLException
{
return Update(tbl,flds,values,null);//认为第一个字段是更新条件,且为数字
}
static public int Update(String tbl,String[] flds,Object[] values,String w) throws IOException, SQLException
{
String sql="";
if(w==null || w == "")//认为第一个字段是更新条件,且为数字
sql="select * from "+tbl+" where "+flds[0]+"="+values[0]+";";
else//只更新结果集中的第一条数据
sql="select * from "+tbl+" where "+w+";";
ResultSet rs=getRs(sql,ResultSet.CONCUR_UPDATABLE);
if(!rs.next()) return 0;
rs.absolute(1);
if(w!=null && w != "") rs.updateObject(flds[0], values[0]);
for(int i=1;i<flds.length;i++){
rs.updateObject(flds[i], values[i]);
}
rs.updateRow();
return 1;
}
static public int Del(String tbl,Object[] ids) throws SQLException, IOException
{
return Del(tbl,"id",ids);
}
static public int Del(String tbl,String fld,Object[] ids) throws SQLException, IOException
{
int i = 0;
PreparedStatement ps = null;
boolean bAuto=conn.getAutoCommit();
String w="",sql;
for(i=0;i<ids.length;i++){
w=w+" "+fld+"="+ids[i]+" or";
}
w=w.substring(0, w.length()-2);
i=0;
sql="delete * from "+tbl+" where "+w;
try {
if(DbType!=0) conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
i = ps.executeUpdate();
if(DbType!=0) conn.commit();
} catch (SQLException e) {
if(DbType!=0) if(conn!=null) conn.rollback();//事务回滚
serr(e.getMessage()+"sql:"+sql);
}
if(ps!=null)ps.close();
if(DbType!=0) conn.setAutoCommit(bAuto);
return i;
}
//关闭数据库连接
static public void close() throws IOException
{
if(conn==null) return;
try{
conn.close();
conn=null;
out=null;
}catch(SQLException e){
serr(e.getMessage());
}
}
//输出错误
static public void serr(Object str) throws IOException{
out.print("<div style='font-size:12px;color:red;padding-top:10px;padding-bottom:10px;'>["+
str+"]</div>");
}
}
jsp:
<%
SqlHelper.init(out,application);
String sTblName="tbClass";
//添加
String[] fld= new String[]{"pid","name"};
Object[] vs= new Object[]{100,"aaaaa"};
SqlHelper.Insert(sTblName,fld,vs);
//更新
fld= new String[]{"pid","name"};
vs= new Object[]{1500,"ccc"};
SqlHelper.serr(SqlHelper.Update(sTblName,fld,vs,"id=125"));
fld= new String[]{"id","pid","name"};
vs= new Object[]{126,1900,"ddd"};
SqlHelper.serr(SqlHelper.Update(sTblName,fld,vs));
//删除
vs=new Object[]{129,130,131};
SqlHelper.serr(SqlHelper.Del(sTblName,vs));
//查
ResultSet rs=SqlHelper.getRs("select * from "+sTblName);
while(rs!=null && rs.next()){
out.print("id:"+rs.getString("id")+"pid:"+rs.getString("pid"));
out.print(" 名字:"+rs.getString("name")+"<br />");
}
SqlHelper.close();rs=null;
%>
1,可以直接在JSP里面写;
2,也可以采用MVC模式,用Servlet+JSP结合 ,因为JSP实际上就是一个Servlet;
3,也可以用JSP标签+JavaBean实现;
给你java bean的示例
package lDB;
import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletContext;
import javax.servlet.jsp.JspWriter;
public class SqlHelper
{
static private Connection conn; //连接对象
static private String dbpath=""; //路径
static private String defaultdbpath="db/maindb.mdb"; //默认路径
static private String dbdriver= "sun.jdbc.odbc.JdbcOdbcDriver";//保存Access驱动程序字符串
static private String connstr="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="; //保存Access连接字符串
static private JspWriter out; //用于错误输出
static private int DbType=0;
//初始化
static public void init(String dbp,JspWriter o,ServletContext app) throws IOException
{
out=o;
dbpath=app.getRealPath(dbp);
getConn();
}
//初始化
static public void init(JspWriter o,ServletContext app) throws IOException
{
dbpath=defaultdbpath;
init(dbpath,o,app);
}
//建立连接
static public Connection getConn() throws IOException{
String cstr="";
try{
if(conn!=null) return conn;
cstr=connstr+dbpath;
Class.forName(dbdriver);
conn = DriverManager.getConnection(cstr);
}catch(Exception e){
conn=null;
serr("SqlHelper.getConn:"+e.getMessage()+";连接字串:"+cstr);
}
return conn;
}
//得到RS
static public ResultSet getRs(String sql) throws IOException{
return getRs(sql,ResultSet.CONCUR_READ_ONLY);
}
static public ResultSet getRs(String sql,int ConcurType) throws IOException{
if(conn == null)
{
serr("SqlHelper.getRs:数据库还没有打开,不能得到rs");
return null;
}
ResultSet rs=null;
try{
Statement stmt=conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,ConcurType);
rs=stmt.executeQuery(sql);
}
catch(SQLException e){
serr(e.getMessage());
}
return rs;
}
static public int Insert(String tbl,String[] flds,Object[] values) throws IOException, SQLException
{
String sql="select * from "+tbl+" where 1=2;";
ResultSet rs=getRs(sql,ResultSet.CONCUR_UPDATABLE);
rs.moveToInsertRow();
for(int i=0;i<flds.length;i++){
rs.updateObject(flds[i], values[i]);
}
rs.insertRow();
return 1;
}
static public int Update(String tbl,String[] flds,Object[] values) throws IOException, SQLException
{
return Update(tbl,flds,values,null);//认为第一个字段是更新条件,且为数字
}
static public int Update(String tbl,String[] flds,Object[] values,String w) throws IOException, SQLException
{
String sql="";
if(w==null || w == "")//认为第一个字段是更新条件,且为数字
sql="select * from "+tbl+" where "+flds[0]+"="+values[0]+";";
else//只更新结果集中的第一条数据
sql="select * from "+tbl+" where "+w+";";
ResultSet rs=getRs(sql,ResultSet.CONCUR_UPDATABLE);
if(!rs.next()) return 0;
rs.absolute(1);
if(w!=null && w != "") rs.updateObject(flds[0], values[0]);
for(int i=1;i<flds.length;i++){
rs.updateObject(flds[i], values[i]);
}
rs.updateRow();
return 1;
}
static public int Del(String tbl,Object[] ids) throws SQLException, IOException
{
return Del(tbl,"id",ids);
}
static public int Del(String tbl,String fld,Object[] ids) throws SQLException, IOException
{
int i = 0;
PreparedStatement ps = null;
boolean bAuto=conn.getAutoCommit();
String w="",sql;
for(i=0;i<ids.length;i++){
w=w+" "+fld+"="+ids[i]+" or";
}
w=w.substring(0, w.length()-2);
i=0;
sql="delete * from "+tbl+" where "+w;
try {
if(DbType!=0) conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
i = ps.executeUpdate();
if(DbType!=0) conn.commit();
} catch (SQLException e) {
if(DbType!=0) if(conn!=null) conn.rollback();//事务回滚
serr(e.getMessage()+"sql:"+sql);
}
if(ps!=null)ps.close();
if(DbType!=0) conn.setAutoCommit(bAuto);
return i;
}
//关闭数据库连接
static public void close() throws IOException
{
if(conn==null) return;
try{
conn.close();
conn=null;
out=null;
}catch(SQLException e){
serr(e.getMessage());
}
}
//输出错误
static public void serr(Object str) throws IOException{
out.print("<div style='font-size:12px;color:red;padding-top:10px;padding-bottom:10px;'>["+
str+"]</div>");
}
}
jsp:
<%
SqlHelper.init(out,application);
String sTblName="tbClass";
//添加
String[] fld= new String[]{"pid","name"};
Object[] vs= new Object[]{100,"aaaaa"};
SqlHelper.Insert(sTblName,fld,vs);
//更新
fld= new String[]{"pid","name"};
vs= new Object[]{1500,"ccc"};
SqlHelper.serr(SqlHelper.Update(sTblName,fld,vs,"id=125"));
fld= new String[]{"id","pid","name"};
vs= new Object[]{126,1900,"ddd"};
SqlHelper.serr(SqlHelper.Update(sTblName,fld,vs));
//删除
vs=new Object[]{129,130,131};
SqlHelper.serr(SqlHelper.Del(sTblName,vs));
//查
ResultSet rs=SqlHelper.getRs("select * from "+sTblName);
while(rs!=null && rs.next()){
out.print("id:"+rs.getString("id")+"pid:"+rs.getString("pid"));
out.print(" 名字:"+rs.getString("name")+"<br />");
}
SqlHelper.close();rs=null;
%>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
期待达人
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询