当前位置:首页 » 数据仓库 » javabean数据库查询
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

javabean数据库查询

发布时间: 2022-07-04 22:41:19

㈠ 在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向服务器发送一个消息,服务器响应

这样方法就多了,发送隐藏表单,是最简单的