㈠ 在jsp中使用JavaBean連接資料庫
這個是連接access的javaBean,首先配置access數據源,數據源名稱設為info.mdb,此文件放在test文件夾下,編譯後將生成的infoBean.class放在claeese下的test文件夾下即可。用access寫個資料庫info.mdb裡面寫個表student,兩個欄位id name。
package test;
import java.sql.*;
public class infoBean{
private String ab="sun.jdbc.odbc.JdbcOdbcDriver";
private String ac="JDBC:odbc:info.mdb";
Connection conn=null;
ResultSet rs=null;
Statement stmt;
public infoBean(){
try{
Class.forName(ab);}
catch(java.lang.ClassNotFoundException e){
System.out.println("infoBean():"+e.getMessage());}
}
public ResultSet executeQuery(String sql){
rs=null;
try{
conn=DriverManager.getConnection(ac);
stmt=conn.createStatement();
rs = stmt.executeQuery(sql);}
catch(SQLException ex){
System.err.println("aq.executeQuery:"+ex.getMessage());}
return rs;
}
public void executeUpdate(String sql)
{stmt=null;
rs=null;
try{
conn=DriverManager.getConnection(ac);
stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();}
catch(SQLException e1)
{System.err.println("executeUpdate:" + e1.getMessage()); }
}
public void closeStmt()
{
try{
stmt.close();
}
catch (SQLException e2)
{
e2.printStackTrace();
}
}
public void closeConn()
{
try{
conn.close();
}
catch (SQLException e3)
{
e3.printStackTrace();
}
}
}
在jsp頁面中引用時,顯示資料庫內容:
<%@ page contentType="text/html;charset=GB2312" import="java.sql.*" %>
<jsp:useBean id="inBean" class="test.infoBean" scope="page"/><html><head></head><body><table>
<tr><th>11</th><th>11</th></tr>
<%
ResultSet rs=inBean.executeQuery("SELECT * FROM student");
while rs.next(){
%>
<tr><td><%=rs.getString(1)%></td><td><%=rs.getString(2)%></td></tr>
<%}%>
</table></body></html>
在jsp中刪除,
<%@ page contentType="text/html;charset=GB2312" import="java.sql.*" %>
<jsp:useBean id="inBean" class="test.infoBean" scope="page"/><html><head></head><body>
<%
String id=request.getParameter("id");
inBean.executeUpdate("Delete from student where id='"+id+"'");
%>
</body></html>
其中id是上一個頁面傳過來的;
jsp中修改:
<%@ page contentType="text/html;charset=GB2312" import="java.sql.*" %>
<jsp:useBean id="inBean" class="test.infoBean" scope="page"/><html><head></head><body>
<%
String id=request.getParameter("id");
String name=request.getParameter("name");
inBean.executeUpdate("update student set name='"+name+"' where id='"+id+"'");
%>
</body></html>
我還有javaBean連接mysql,sql server的例子,也有關於javaBean完整簡單的小項目,你要的話,給郵箱,發給你
㈡ javabean中的資料庫的自動編號欄位查詢問題
int id=Integer.parseInt(你接收來的id變數名);
㈢ javabean查詢資料庫
userlist.add(userone)吧應該是
㈣ 怎麼用javabean查詢資料庫
如果想用javabean的形式查詢資料庫的話,你可以試試mybatis技術,他給你集成了很多實用的功能,利用參數拼接等等.
㈤ JSP使用javabean查詢資料庫
使用useBean標簽定義的對象不能直接在jsp小腳本中使用。
useBean定義的對象相當於把定義的對象在了page/request/session/application的這些個范圍內,
JSP小腳本中要使用可以從這些范圍內先取出然後再用
如下:
JDBCBean jdbc=pageContext.getAttribute(「jdbc」);
當然要記得導包啊
㈥ 用javabean怎麼樣才能顯示資料庫中的數據,(Java與資料庫已通過jdbc在另已在中連接 )
資料庫中有個學生表(STUDENT),欄位有學號、姓名、性別、生日。
1、建立學生對應的JavaBean
publicclassStudent{
privateintno;
privateStringname;
privateintsex;
privateDatebirthday;
//setter、getter方法
}
2、從資料庫中查詢數據封裝到JavaBean中
Stringsql="select*fromstudent";
ps=connection.preparestatement(sql);
rs=ps.execeteQuery();
ArrayList<Student>list=newArrayList<Student>();
while(rs.hasNext()){
Students=newStudent();
s.setNo(rs.getInt("NO"));
//...
list.add(s);
}
//然後遍歷list就可以拿到Student的數據了。
㈦ 創建javabean 實現資料庫的鏈接 查詢 修改 刪除 在jsp中調用javabean 。實現用
意思就是要在jsp頁面上寫java代碼?這個需要導bean包,就是在jsp頭裡面寫import="java.util.*(這個應該默認是有的),需要導的bean包"
多嘴說一句,在頁面寫代碼....現在應該沒有哪個公司會允許你這么乾的
㈧ mysql+jsp+javabean實現資料庫數據查詢
有提問題的時間,我看早就能把這個東西寫完了,何況都知道MVC了,這個還不會?
㈨ 如何在JS中通過JAVABEAN提取資料庫數據
測試的電腦已經裝好Oracle客戶端,而且用SQLplus可以連接上。
/*
* This sample shows how to list all the names from the EMP table
*
* It uses the JDBC THIN driver. See the same program in the
* oci8 samples directory to see how to use the other drivers.
*/
// You need to import the java.sql package to use JDBC
import java.sql.*;
class Test
{
public static void main (String args [])
throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
/* try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(Exception e){
System.out.println("No Driver!");
}
*/
// Connect to the database
// You must put a database name after the @ sign in the connection URL.
// You can use either the fully specified SQL*net syntax or a short cut
// syntax as <host>:<port>:<sid>. The example uses the short cut syntax.
String url = "jdbc:oracle:thin:@172.28.31.85:1521:YIKATONG";
String userName = "scott";
String password = "tiger";
if (args.length > 0) url = args[0];
if (args.length > 1) userName = args[1];
if (args.length > 2) password = args[2];
System.out.println(url);
System.out.println(userName);
System.out.println(password);
Connection conn =
DriverManager.getConnection (url, userName, password);
// Create a Statement
Statement stmt = conn.createStatement ();
// Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select * from Test");
// Iterate through the result and print the employee names
while (rset.next ())
System.out.println (rset.getString (1));
}
}
補充日期: 2005-03-14 20:20:29
Java與Oracle的兩種連接方式
src=http://pagead2.googlesyndication.com/pagead/show_ads.js>(作者:huihoo)
第一種方式:通過資料庫本身的JDBC Driver連接到資料庫
Classs.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.33:1521:huihoo","scott","tiger");
第二種方式:通過JDBC-ODBC橋連接到資料庫
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:192.168.1.33","scott","tiger");
192.168.1.33為數據源
完整的用戶登錄
Properties props = new Properties();
props.put("user", "scott");
props.put("password", "tiger");
Driver myDriver = (Driver) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = myDriver.connect("jdbc:oracle:thin:@192.168.1.33:1521:huihoo", props);
conn.close();
System.out.println("成功登錄.");
System.out.println("歡迎您 "+props.getProperty("user")+"!");
㈩ 關於javascript能否調用javabean查詢資料庫
咋可能。。。。。。。。。。
javascript是HTML的一部分。。
如果要用java代碼去響應javascript的話,那必須進行一次交互,用javascript向伺服器發送一個消息,伺服器響應
這樣方法就多了,發送隱藏表單,是最簡單的