jsp连接数据库的代码

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连接数据库的代码

知识

罗梅芬用日文怎么写(罗钰潇日语怎么写)

阅读(21433)

本文主要为您介绍罗梅芬用日文怎么写,内容包括伊蕾娜日语怎么写,王雪菲用日文怎么说,张佳怡在日语中怎么写啊怎么读啊。罗 ら ラ ra钰 ぎょく ギョク gyoku潇 しょう シヨウ shou第一列:日语汉字,写法同汉字,都要用繁体,这三个都挺难写的,看

知识

邓先生的英文怎么写(1~40的英文怎么说)

阅读(10450)

本文主要为您介绍邓先生的英文怎么写,内容包括“邓先生”用英语怎么写,1~40的英文怎么说,漂亮英文beautiful缩写怎么写。1 one 2 two 3 three 4 four 5 five 6 six 7 seven 8 eight 8 nine 10 te

知识

一个人布满皱纹怎么写(描写人物皱纹的句子)

阅读(9528)

本文主要为您介绍一个人布满皱纹怎么写,内容包括描写人物皱纹的句子,描写人物皱纹的句子,皱纹怎么描写。、老人脸上布满了皱纹,那一条条曲折不均的像是墙上斑驳的印迹,爬满了面容,留下了岁月的痕迹。2、外祖父是一位年过六旬的白发老人。在他

知识

登录接口怎么写(php登录的接口怎么写)

阅读(7769)

本文主要为您介绍登录接口怎么写,内容包括php登录的接口怎么写,网页登陆接口怎么做,网站登录接口程序怎么做。PHP 接口 接口 使用接口(interface),你可以指定某个类必须实现哪些方法,但不需要定义这些方法的具体内容。我们可以通过int

知识

档案奖惩情况怎么写(奖惩情况怎么写)

阅读(9609)

本文主要为您介绍档案奖惩情况怎么写,内容包括奖惩情况怎么写,个人简历及奖惩情况怎么填写,个人简历里面奖惩情况怎么写。在简历里的“奖励”部分,列出与你所获得的并与你的求职目标相关的荣誉、奖励和奖金。你既可以按时间顺序排列,也可以按

知识

头孢克肟拼音怎么写(头孢克肟的肟念什么)

阅读(8012)

本文主要为您介绍头孢克肟拼音怎么写,内容包括头孢克肟片全名拼音,头孢克肟片全名拼音,头孢克肟的肟念什么。肟[wò] :是含有羰基的醛、酮类化合物与羟胺作用而生成的有机化合物,可以参与许多有机化学反应,例如经典的Beckmann重排就是肟为底

知识

一库搜用日语怎么写(日语一库是什么意思)

阅读(8006)

本文主要为您介绍一库搜用日语怎么写,内容包括日语大神来,看动漫里的主人公说一句:恰,一库搜这是什么意思,一库一库;一搜库这两个日语是什么意思怎么写,看片都有“一库”(日语)是什么意。一库的意思就是“出发,出去”的意思。日语「行く」的音译

知识

外租无人机广告怎么写(植保无人机广告语)

阅读(7009)

本文主要为您介绍外租无人机广告怎么写,内容包括求一个无人机创意广告词谢谢巨友们了,求一关于无人机的广告标语,求一关于无人机的广告标语我们公司是做无人机的,新成立的公司,求。DJI大疆创新研发的的MG-1农业植保机专为农村作业环境设计,

知识

河南话que怎么写(河南话的nenna怎么写)

阅读(6541)

本文主要为您介绍河南话que怎么写,内容包括que怎么写,河南话的nenna怎么写,que怎么写。尿一壶(niào yī hú)关系密切,观点一致。例:“他俩今天尿一壶啦。”●尿(niào)⑴、从尿道排泄的液体。⑵、排泄小便。⑶、不放

知识

国学经文的论文怎么写(国学征文该怎么写)

阅读(7122)

本文主要为您介绍国学经文的论文怎么写,内容包括国学征文该怎么写,弟子规的400论文,关于国学经典的征文怎么写。“子曰:“温故而知新,可以为师”……小时,总是觉得国学就是没用的,古人写的话,我们还需要背,每次老师教给我们时,我总是会让思想开一

知识

化学实验总结怎么写(化学实验报告小结怎么写)

阅读(5406)

本文主要为您介绍化学实验总结怎么写,内容包括化学实验总结怎么写,化学实验报告小结怎么写,化学实验小结怎么写。化学实验报告的书写: 一般情况下化学实验报告是根据实验步骤和顺序从七方面展开来写的: 1.实验目的:即本次实验所要达到的目标或

知识

蝴蝶豌豆拼音怎么写(豌豆的拼音是什么)

阅读(5834)

本文主要为您介绍蝴蝶豌豆拼音怎么写,内容包括蝴蝶怎么拼音的,豌豆的拼音是什么,蝴蝶的拼音是什么。豌豆的拼音是[wān dòu]。豌豆是豆科一年生攀援草本,高0.5-2米。全株绿色,光滑无毛,被粉霜。叶具小叶4-6片,托叶心形,下缘具

知识

海绵宝宝用英文怎么说(海绵宝宝用英文怎么说)

阅读(6519)

本文主要为您介绍海绵宝宝用英文怎么说,内容包括海绵宝宝用英语怎么说,海绵宝宝用英文怎么说,海绵宝宝英文名是什么。1. SPONGEBOB SQUAREPANTS 近期很夯的一步卡通影片《海绵宝宝》(SpongeBob SquarePants)是一系

知识

茶盏怎么用(茶盏在茶道中干嘛用)

阅读(5428)

本文主要为您介绍茶盏怎么用,内容包括茶盏怎么用我要写一篇200字左右的茶盏的使用说明,求指教,茶盏在茶道中干嘛用,问一下斗笠盏如何使用现在是不是很少有人使用它,它的意义。苏东坡的名句"从来佳茗似佳人",典型地代表了唐宋及以后的文人墨客,

知识

thinkpad小红点怎么用(怎么学习使用thinkpad小红点)

阅读(7592)

本文主要为您介绍thinkpad小红点怎么用,内容包括怎么学习使用thinkpad小红点,thinkpad小红点怎么用,求教:THINKPAD的小红点使用方法。Thinkpad 小红点最高效的使用方法为:左手拇指按左键,无操作时在左键待命2、右手拇指按右键,同时兼按空格键及