当前位置:懂科普 >

综合知识

> jsp数据库代码怎么写

jsp数据库代码怎么写

1. jsp连接数据库代码怎么写

给你一个javabean吧

jsp数据库代码怎么写

package openjdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class openjdbc {

private String url = "jdbc:sqlserver://localhost:1433;DatabaseName=学生信息管理"; //数据库名

private String userName = "sa"; //数据库用户名

private String password = "Lui1988*"; //数据库密码

private ResultSet rs;

private Connection conn = null; //连接对象

private Statement stmt = null;

public openjdbc()

{

try {

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

} catch (ClassNotFoundException e) {

System.err.println("openjdbc():"+e.getMessage());

}

}

public ResultSet executeQuery(String sql)

{

try

{

conn = DriverManager.getConnection(url, userName, password);

stmt=conn.createStatement();

rs=stmt.executeQuery(sql);

}

catch(SQLException e)

{

System.err.println("rs.exectueQuery:"+e.getMessage());

}

return rs;

}

}

相应的数据自己改下,这个得要数据库的jdbc驱动!

2. jsp连接数据库代码填写

try{Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=WebExam";Connection conn = DriverManager.getConnection(url,"sa","123456");Statement stmt = conn.createStatement();String sql = "select * from tb_user where userName='"+sUserNam+"' and password='"+sPWD+"' ";ResultSet rs = stmt.executeQuery(sql);if (!rs.next()) out.println("用户名或密码错误!");else response.sendRedirect("index.jsp");}。

3. 怎样用jsp连接mysql数据库 代码怎么写啊

Class.forName("com.mysql.jdbc.Driver");

Connection con= DriverManager.getConnection("jdbc:mysql://10.10.2.188:3306/bbs?useUnicode=true&characterEncoding=utf8",

"root","yaoweijq");

Statement stat= con.createStatement();

第一句是加载数据库驱动,

自己下一个加到类库中,

我不知道要不要放到tomcat目录下。

第二句是连接数据库,

IP自己改,测试用localhost

3306是端口,mysql的

后面是数据库名你也可以用表名

再后面root是用户名yaoweijq是密码 都是mysql的

大体就是这样

4. JSP连接数据库要有几种方法,要怎么连接

一、jsp连接Oracle8/8i/9i数据库(用thin模式

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>

二、jsp连接Sql Server7.0/2000数据库

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>

5. jsp查询数据库代码

这种方法并不是很常用,只是页面的相关操作,代码太多,所以中间省略了部分 <%Object userUID=session.getAttribute("USERID");String uid=""; if(userUID!=null){ uid=(String)userUID; }else{ response.sendRedirect("../Public/loseSession.jsp");} try{String today=DateUtils.getInstance().getToday();String userIP=request.getRemoteAddr();String sql="";String getPage=request.getParameter("toPage"); sql=cu.exchange(request.getParameter("sql")); if (sql==null||sql.equals("")){String getDepId="";String getPoliticalPosition="";String getTechnicalPosition="";String getPoliticalLevel="";String getTechnicalLevel="";String getDegreeCode="";String getGrade="";String getLongevity=""; String getAllowance="";long depId=0;long degreeCode=0;long politicalPosition=0;long technicalPosition=0;long politicalLevel=0;long technicalLevel=0;long grade=0;long longevity=0;long allowance=0;String[] postID=null;String post="";String userID="";String userName="";String address="";getDepId=request.getParameter("depId");depId=Long.parseLong(getDepId.trim()); getDegreeCode=request.getParameter("degreeCode");if (getDegreeCode!=null) {degreeCode=Long.parseLong(getDegreeCode.trim());}getPoliticalPosition=request.getParameter("politicalPosition");if (getPoliticalPosition!=null) {politicalPosition=Long.parseLong(getPoliticalPosition.trim()); }post=request.getParameter("post");if(post!=null&&post.trim().equals("1")) postID=request.getParameterValues("dyourlocation");getTechnicalPosition=request.getParameter("technicalPosition");if (getTechnicalPosition!=null) {technicalPosition=Long.parseLong(getTechnicalPosition.trim()); }getPoliticalLevel=request.getParameter("politicalLevel");if (getPoliticalLevel!=null) { politicalLevel=Long.parseLong(getPoliticalLevel.trim()); } getTechnicalLevel=request.getParameter("technicalLevel");if (getTechnicalLevel!=null) {technicalLevel=Long.parseLong(getTechnicalLevel.trim()); } getGrade=request.getParameter("grade");if (getGrade!=null) {grade=Long.parseLong(getGrade.trim()); } getLongevity=request.getParameter("longevity");if (getLongevity!=null) {longevity=Long.parseLong(getLongevity.trim()); } getAllowance=request.getParameter("allowance");if (getAllowance!=null) { allowance=Long.parseLong(getAllowance.trim());}userID=ParamUtils.getParameter(request,"userID");//用户代码userName=ParamUtils.getParameter(request,"userName");//用户名称address=ParamUtils.getParameter(request,"address");//得到要转入的页面 sql="select org_user.* from org_user,org_detail where org_user.user_id=org_detail.user_id";if (depId!=0){ sql=sql+" and org_user.department_id="+depId+"";} if (userID!=null&&userID!="") { sql=sql+" and org_user.user_id='"+userID+"'";} if (userName!=null&&userName!=""){ sql=sql+" and org_user.name like '%"+userName+"%'";} if (address!=null&&address!="") { sql=sql+" and org_user.address like '%"+address+"%'"; }if(post!=null&&!post.trim().equals("0")) { sql=sql+" and org_detail.post in ("; for(int i=0;i0) { if (cnt%pageSize==0) cntPage=cnt/pageSize; else cntPage=cnt/pageSize+1; } else cntPage=0; if (getPage==null) { getPage="1"; curPage=1; } else curPage=Integer.parseInt(getPage.trim());%>

6. 在jsp页 面中对数据库的增删改代码怎么写,需要在jsp页面中写java语

public int insertArticle(Article article){//添加 try {Connection connection =null;PreparedStatement preparedStatement=null;Article article=null;connection=JDBCUtils.getConnection(); String sql="insert into article (title,content,publishTime,typeId,userId) values(?,?,?,?,?)"; preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1, article.getTitle()); preparedStatement.setString(2, article.getContent()); preparedStatement.setString(3, article.getPublishTime()); preparedStatement.setInt(4, article.getTypeId()); preparedStatement.setInt(5, article.getUserId()); return preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return 0; }finally{ JDBCUtils.release(connection, preparedStatement, null); } } public Article queryArticle(int articleId) { Connection con = JDBCUtils.getConnection(); ; PreparedStatement ps = null ; Article article=null; ResultSet rs = null; String sql = "select * from article where articleId='" + articleId + "'"; try { ps=con.prepareStatement(sql); rs = ps.executeQuery(sql); while (rs.next()) { article= new Article(); article.setArticleId(rs.getInt(1)); article.setTitle(rs.getString(2)); article.setContent(rs.getString(3)); article.setPublishTime(rs.getString(4)); article.setUserId(rs.getInt(5)); article.setTypeId(rs.getInt(6)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return article; }public int deleteArticle(int articleId){//删除 Connection conn=null; PreparedStatement pstmt=null; try { String sql="delete from article where articleId='" + articleId+ "'"; conn=JDBCUtils.getConnection(); pstmt=conn.prepareStatement(sql); int rtn=pstmt.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCUtils.release(conn, pstmt, null); } return 0; } public int updateArticle(Article article){//更新 Connection conn=null; PreparedStatement pstmt=null; int articleId=article.getArticleId(); try { String sql="update article set title=?,content=?,publishTime=?,typeId=? where articleId='" + articleId+ "'"; conn=JDBCUtils.getConnection(); pstmt=conn.prepareStatement(sql); pstmt.setString(1, article.getTitle()); pstmt.setString(2, article.getContent()); pstmt.setString(3, article.getPublishTime()); pstmt.setInt(4, article.getTypeId()); int rtn=pstmt.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCUtils.release(conn, pstmt, null); } return 0; }你自己改改数据bean就能用了,jdbc的连接你自己写写。

7. jsp怎么写增删改查代码

下面的代码即可实现(对数据库的操作):<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@page import="java.sql.*"%>

管理中心


<% // 数据库的名字 String dbName = "zap"; // 登录数据库的用户名 String username = "sa"; // 登录数据库的密码 String password = "123"; // 数据库的IP地址,本机可以用 localhost 或者 127.0.0.1 String host = "127.0.0.1"; // 数据库的端口,一般不会修改,默认为1433 int port = 1433; String connectionUrl = "jdbc:sqlserver://" + host + ":" + port + ";databaseName=" + dbName + ";user=" + username + ";password=" + password; // //声明需要使用的资源 // 数据库连接,记得用完了一定要关闭 Connection con = null; // Statement 记得用完了一定要关闭 Statement stmt = null; // 结果集,记得用完了一定要关闭 ResultSet rs = null; try { // 注册驱动 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 获得一个数据库连接 con = DriverManager.getConnection(connectionUrl); String SQL = "SELECT * from note"; // 创建查询 stmt = con.createStatement(); // 执行查询,拿到结果集 rs = stmt.executeQuery(SQL); while (rs.next()) { %> <% } } catch (Exception e) { // 捕获并显示异常 e.printStackTrace(); } finally { // 关闭我们使用过的资源 if (rs != null) try { rs.close(); } catch (Exception e) {} if (stmt != null) try { stmt.close(); } catch (Exception e) {} if (con != null) try { con.close(); } catch (Exception e) {} } %>
ID 书名 作者 价格 删除
<%=rs.getInt(1)%> " target="_blank"><%=rs.getString(2)%> <%=rs.getString(3)%> <%=rs.getString(4)%> " target="_blank">删除
jsp">添加新纪录

标签: JSP 代码 数据库
  • 文章版权属于文章作者所有,转载请注明 https://dongkepu.com/zonghezhishi/kmeykk.html