在实体bean中如何连接数据库 在 The J2EE Tutorial 中‘saveascount’例子 不知道如何连接到数据库,如果数据库选择sql server该怎么连接?谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我用的方法是专门编写了一个数据库的连接bean,在bean生成一个静态方法,用bean的实例调用它就能返回一个connection对象完成数据库连接。 嗨 java之路困难重重啊!! public DataSource getDataSource(){ DataSource dataSource = null; try{ dataSource = (DataSource)ic.lookup("DataSource");//jndi名字 }catch(Exception e){ e.printStackTrace(); } return dataSource; } 这是一个用jdbc驱动连接sql server2000的javabean;你可以试试看package 包名;import java.sql.*;public class bizconn{ String szDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; String szConn="jdbc:microsoft:sqlserver://SQLSERVER 所在電腦的IP地址:1433;DatabaseName=庫名"; Connection con=null; ResultSet rs=null; Statement stmt_qry=null; Statement stmt_exec=null; public bizconn() throws Exception{//ClassNotFoundException try{ Class.forName(szDriver); con=DriverManager.getConnection(szConn,"用戶名username","數據庫口令password"); stmt_qry=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); stmt_exec=con.createStatement(); } catch(Exception e){ System.err.println(e.getMessage()); } } public ResultSet execQry(String szSQL){ //Execute Query rs=null; try{ rs=stmt_qry.executeQuery(szSQL); } catch(SQLException ex){ System.err.println(ex.getMessage()); } return rs; } public void execUpdate(String szSQL){ //Execute INSERT,UPDATE,DELETE try{ stmt_exec.executeUpdate(szSQL); } catch(SQLException ex){ System.err.println(ex.getMessage()); } } public void destroy(){ try{ if(stmt_qry!=null) stmt_qry.close() ; if(stmt_exec!=null) stmt_exec.close(); if(con!=null) con.close() ; } catch(Exception e){ e.printStackTrace() ; } }]下面是在jsp中如何使用的一个简单的例子:使用(*.jsp):<%@ page language="java" import="java.sql.*"%><jsp:useBean id="vSql" scope="page" class="包名.bizconn"/><%查詢ResultSet rs=vSql.execQry(SQL查詢語句);插入更新刪除vSQL.execUpdate(SQL執行語句);%> to: free111(浪子天涯)jndi名字如何设置连接不同的数据库?,现在只是照猫画虎不知所然public DataSource getDataSource(){ DataSource dataSource = null; try{ dataSource = (DataSource)ic.lookup("DataSource");//jndi名字 }catch(Exception e){ e.printStackTrace(); } return dataSource; } to liangzai6640459(亮仔) 就是不知道在 实体bean 中如何通过jdbc连接数据库 使用jndi.getConnection()或者driverManager.getConnection()得到连接然后通过cmp 或者bmp对database操作 JAVA面试填空题 jsp问题 关于多线程方法级同步和代码块级同步的问题 The requested resource is not available. webwork2文件下载中出现中文名字错误~ 请大家帮我看一下啊! xfire问题! 请各位帮忙看看,给个高分!!! 问个事儿啊,前辈说说,现在大型的人力资源管理软件,一般用神马架构 RMI的问题,为什么不能自动下载类? EJB-QL语句求教 这个错误捕捉不到 请问在JAVA里如何将字符型转化为日期型? 有j2se的基础学好j2ee需要多长时间
DataSource dataSource = null;
try{
dataSource = (DataSource)ic.lookup("DataSource");//jndi名字 }catch(Exception e){
e.printStackTrace(); }
return dataSource; }
package 包名;
import java.sql.*;public class bizconn
{
String szDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String szConn="jdbc:microsoft:sqlserver://SQLSERVER 所在電腦的IP地址:1433;DatabaseName=庫名";
Connection con=null;
ResultSet rs=null;
Statement stmt_qry=null;
Statement stmt_exec=null; public bizconn() throws Exception{//ClassNotFoundException
try{
Class.forName(szDriver);
con=DriverManager.getConnection(szConn,"用戶名username","數據庫口令password");
stmt_qry=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt_exec=con.createStatement();
}
catch(Exception e){
System.err.println(e.getMessage());
}
} public ResultSet execQry(String szSQL){ //Execute Query
rs=null;
try{
rs=stmt_qry.executeQuery(szSQL);
}
catch(SQLException ex){
System.err.println(ex.getMessage());
}
return rs;
} public void execUpdate(String szSQL){ //Execute INSERT,UPDATE,DELETE
try{
stmt_exec.executeUpdate(szSQL);
}
catch(SQLException ex){
System.err.println(ex.getMessage());
}
} public void destroy(){
try{
if(stmt_qry!=null) stmt_qry.close() ;
if(stmt_exec!=null) stmt_exec.close();
if(con!=null) con.close() ;
}
catch(Exception e){
e.printStackTrace() ;
}
}]
下面是在jsp中如何使用的一个简单的例子:
使用(*.jsp):
<%@ page language="java" import="java.sql.*"%>
<jsp:useBean id="vSql" scope="page" class="包名.bizconn"/><%
查詢
ResultSet rs=vSql.execQry(SQL查詢語句);插入更新刪除
vSQL.execUpdate(SQL執行語句);
%>
jndi名字如何设置连接不同的数据库?,现在只是照猫画虎不知所然
public DataSource getDataSource(){
DataSource dataSource = null;
try{
dataSource = (DataSource)ic.lookup("DataSource");//jndi名字 }catch(Exception e){
e.printStackTrace(); }
return dataSource; }
就是不知道在 实体bean 中如何通过jdbc连接数据库
jndi.getConnection()
或者driverManager.getConnection()得到连接
然后通过cmp 或者bmp对database操作