關閉→
當前位置:知科普>綜合知識>jsp數據庫代碼怎麼寫

jsp數據庫代碼怎麼寫

知科普 人氣:2.44W
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">添加新紀錄

TAG標籤:#JSP #代碼 #數據庫 #