jsp数据库代码怎么写

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

给你一个javabean吧

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数据库代码怎么写

知识

拼音d怎么写笔顺

阅读(1045)

本文主要为您介绍拼音d怎么写笔顺,内容包括d的笔顺怎么写,d的拼音笔画顺序是什么,拼音d的笔顺是先写圈还是先写竖。d的拼音笔画顺序是第一笔左半圆,第二笔竖,如下图: 延伸: 汉语拼音共有十种基本笔画,它们的名称是: 横、竖、左弯竖、右弯竖、竖

知识

发的书法怎么写

阅读(201)

本文主要为您介绍发的书法怎么写,内容包括发在书法作品中怎么写,发在书法作品中怎么写,行书的发字怎么写。想写好字:要有极高的兴趣 还有天赋 有自己的创意 不要死学 可以抱一些学习班 推荐字体:颜真卿的多宝塔 最好软笔硬笔一块学习 对你有

知识

耳字繁体怎么写

阅读(198)

本文主要为您介绍耳字繁体怎么写,内容包括耳的繁体字怎么写,耳繁体字怎么写,耳繁体字怎么写。

知识

落地证怎么写

阅读(198)

本文主要为您介绍落地证怎么写,内容包括落地证明怎么写,怎么写申请户口落地证,请问户口迁移申请书怎么写啊。证明格式怎么写 实习证明格式范文一 兹有 学校 同学于 年 月 日至 年 月 日在 大学生就业实习基地实习。特此证明。武汉大学生就

知识

怎么写开学啦

阅读(227)

本文主要为您介绍怎么写开学啦,内容包括开学了作文怎么写,现在开学怎样写以“开学了”为题的作文,开学了作文怎么写。开学感想寒假终于都过去了,紧接着就是读书了.六年纪,已经是毕业班了,我不敢疏忽,心想着努力读书,不过好像做不到. 新的学期,新

知识

竿的拼音怎么写的

阅读(354)

本文主要为您介绍竿的拼音怎么写的,内容包括竿,这拼音怎么写,竿鲒的拼音怎么写的,竿的拼音意思及造句。竿 [ gān ] 释义竿 [ gān ]:竹子的主干;特指“钓鱼竿”;古代指竹简 组词竹竿 [ zhú gān ]:砍下来的削去枝叶的

知识

月计划本怎么写

阅读(178)

本文主要为您介绍月计划本怎么写,内容包括月计划怎么写,助理的周总结周计划月总结月计划怎么写啊,训练月计划怎么写。原发布者:梦落轩痕【月度工作计划怎么写】为什么要写工作计划工作计划是各级管理人员提高工作效率的有效手段。写工作计划

知识

全力怎么写

阅读(174)

本文主要为您介绍全力怎么写,内容包括竭尽全力作文怎么写,照样子写词语权力――全力还能怎么写词语,我们向你保证,我们会尽全力保护你英语怎么写。“你只是尽力,而我竭尽全力”在生活着无数羚羊的大草原上来了一群狼,狼的出现打乱了羚羊们平

知识

转正的工作总结怎么写

阅读(211)

本文主要为您介绍转正的工作总结怎么写,内容包括试用期转正工作小结怎么写,要一百个字谢谢,很急,试用期转正工作总结怎么写,转正工作总结。给你两份,参考一下吧,希望对你有帮助 员工转正申请书 尊敬的领导: 我于2004年7月9日成为公司的试用员

知识

写求职自我介绍怎么写

阅读(173)

本文主要为您介绍写求职自我介绍怎么写,内容包括书面自我介绍怎么写(求职用),求职自我介绍该怎么写自我介绍怎么写,求职自我介绍怎么写。参考一:本人为人诚挚守信,待人热情。性格开朗、温和、善良、稳重、有活力;对事业有强烈的责任心和使命感,并

知识

新闻的信怎么写

阅读(208)

本文主要为您介绍新闻的信怎么写,内容包括预约信怎么写,新闻上的,怎样写感谢信的新闻稿,怎么写新闻作文300字。第一行的正中用较大的字体写上“感谢信”三个字。如果写给个人,这三个字可以不写。有的还在“感谢信”的前边加上一个定语,说明是

知识

1到20单词怎么写

阅读(189)

本文主要为您介绍1到20单词怎么写,内容包括1到20的英语单词怎么写,1到20的单词怎么写,一到二十的英语单词怎么写。1 one 2 two 3 three4 four5 five6 six7 seven 8 eight9 nine10 ten 11 e

知识

朱篆书怎么写

阅读(262)

本文主要为您介绍朱篆书怎么写,内容包括朱篆书怎么写,朱的篆书怎么写,朱紫玲印的篆书怎么写。朱的篆书:朱的读音:baizhū二、汉字释义:[ zhū ] 1.红色。2.矿物名。du3.姓。[ shú ] 〔~提(shí)〕古地名

知识

阅读趣事怎么写

阅读(230)

本文主要为您介绍阅读趣事怎么写,内容包括我的读书趣事作文怎么写,关于读书趣事的作文怎么写了,小学二年级写一篇读书的趣事,怎么写请指教.。我读书 我快乐 我成长 儿时的我,在“锄禾日当午,汗滴何下土”的诗歌声中长大,每次端起书,看着那毫无

知识

搜索的代码怎么写

阅读(168)

本文主要为您介绍搜索的代码怎么写,内容包括c++按文件后缀搜索的代码怎么写,PHP的搜索的代码是怎样写的,asp.net中站内搜索的代码怎么写。<?php$conn=mysql_connect(localhost,数据库用户名,数据库密码);//连接数据库mysql_select

知识

注册代码怎么写

阅读(154)

本文主要为您介绍注册代码怎么写,内容包括怎么写网站注册代码,用c语言写个注册登录的代码怎么写,手机注册ID时代码怎么填。递一个参数,是数据库中的关键字段,比如ID,用户名。 注意要把这个参数在用户修改后同时提交到处理程序中 然后打开数据

知识

函数代码怎么写的

阅读(184)

本文主要为您介绍函数代码怎么写的,内容包括怎样写一个函数代码,Excel函数代码怎么写,excel统计的批注数据(内容)的函数代码怎么写。统计有多少个单元格吗?比如统计批注中包含“中国”的单元格个数。用自定义函数。第一:ALT+F11,插入-模块 第二:

知识

网页个代码怎么写

阅读(197)

本文主要为您介绍网页个代码怎么写,内容包括HTML代码怎么写,用代码直接编写网页的步骤是什么,网页代码怎么样写啊那位朋友交交。做网页的,HTML必须精通。当然咯,只要记住各种标签的名字,属性及功能就行了。如果不熟悉html,那么你如果做动态网页

知识

怎么写电脑代码

阅读(172)

本文主要为您介绍怎么写电脑代码,内容包括什么是代码,代码怎么写,怎样写入电脑,代码在电脑上怎么写,计算机代码是什么。代码指的是程序员用开发工具所支持的语言写出来的源文件,是一组由字符、符号或信号码元以离散形式表示信息的明确的规则

知识

居中的代码怎么写

阅读(146)

本文主要为您介绍居中的代码怎么写,内容包括html居中代码怎么写,div居中代码怎么写,css居中代码怎么写才能实现。如果只是水平居中的话,只需要先定义DIV的宽度,再设置外边距margin属性的左右边距为auto即可。此时DIV便会相对于它的父容器水平

知识

怎么写源代码

阅读(171)

本文主要为您介绍怎么写源代码,内容包括什么是代码,代码怎么写,怎样写入电脑,怎么编程写代码,如何学会写代码。代码指的是程序员用开发工具所支持的语言写出来的源文件,是一组由字符、符号或信号码元以离散形式表示信息的明确的规则体系。简

知识

html文字代码怎么写

阅读(204)

本文主要为您介绍html文字代码怎么写,内容包括求一段完整的html语言的文字代码,请教个网页文字代码,html代码怎么写。<body><p align="center"><Font face="黑体" size="7" color="black"<s>Mic

[/e:loop]