补充点:我的配置 2000专业系统+jakarta-tomcat-5.0.16+JDK1.2.2
解决方案 »
- 请问一个很简单的小问题???急用
- 谁能帮我把这个代码写成界面形式的?
- TOMCAT6访问页面问题
- fzlotuscn (无为一本)(真名林游)已经被封了ID但换了马甲NMDJSP() ,希望大家警惕这人,他还在到处乱发骗钱!
- servlet布署到JSP项目的问题?
- 高分请求知识一个JSP论坛,仿动网,可以运行在Linux下的
- 在线等急………~~~
- odbc如何设置呢
- jsp中存取(oracle)Blob類型數據﹖﹖﹖
- springboot maven多模块启动问题。
- 微软的老大比尔盖茨大学读了两年就退学了,学历也没有拿到。然而仰视今天的全球微软,还需要学历来证明其能力吗?可为什么中国的招聘都要
- 有哪位懂自定义标签的朋友(我已经最简化了),麻烦指教,正解必给分
-->
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/mysql</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/mysql</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password/>
</local-tx-datasource>
</datasources>
private static boolean nFirstTime=true;
private Connection conn;
try
{
Context initCtx = new javax.naming.InitialContext();
ds =(DataSource)initCtx.lookup(HttpParams.getString(Constants.DATASOURCE));
}
catch (NamingException e)
{
log.error(e.getMessage());
} try
{
conn=ds.getConnection();
}
catch(Exception exp )
{
log.error(exp.getMessage());
}
{
Statement stmt;
ResultSet rst;
rst = null;
try
{
stmt=conn.createStatement();
rst = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
log.error("Query error:"+sql);
}
return rst;
}public void executeUpdate(String sql) throws SQLException
{
Statement stmt;
ResultSet rst;
try
{
stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
}
catch(SQLException ex)
{
log.error("Update error:"+sql);
throw ex;
}
}
还是用xml来设置连接
import java.io.*;
import java.util.*;public class JdbcBean{
private String driver="sun.odbc.JdbcOdcDriver";
private String url="jdbc:odbc:chatroom";
private String query=""; //查询的SQL命令
private String sql="";
private Connection connection=null;
private Statement statement=null;
private ResultSet rs=null;
// public JdbcBean(){}
public JdbcBean(){
try{
Class.forName(driver);
}
catch(ClassNotFoundException cnfe){
System.out.println("Message:"+cnfe.getMessage() );
}
catch(Exception ex){
ex.printStackTrace();
}
}
public ResultSet getRs(String query){ //执行查询命令,得到一个结果集
try{
connection=DriverManager.getConnection(url,"sa","");
Statement statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=statement.executeQuery(query);
}
catch(SQLException ex){
System.out.println("\nERROR:---- SQLException ----\n");
while(ex!=null){
System.out.println("Message: "+ex.getMessage());
System.out.println("SQLState: "+ex.getSQLState());
System.out.println("ErrorCode: "+ex.getErrorCode());
ex=ex.getNextException();
}
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
try{
if(statement !=null){ statement.close(); }
if(connection !=null){ statement.close(); }
}
catch(SQLException ex){
System.out.println("\nERROR:---- SQLException ----\n");
while(ex!=null){
System.out.println("Message: "+ex.getMessage());
System.out.println("SQLState: "+ex.getSQLState());
System.out.println("ErrorCode: "+ex.getErrorCode());
ex=ex.getNextException();
}
}
}
return rs;
}
public void update(String sql){ //执行一个更新命令,实行对数据的删除,修改和插入
try{
connection=DriverManager.getConnection(url,"sa","");
Statement statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
statement.executeUpdate(sql);
}
catch(SQLException ex){
System.out.println("\nERROR:---- SQLException ----\n");
while(ex!=null){
System.out.println("Message: "+ex.getMessage());
System.out.println("SQLState: "+ex.getSQLState());
System.out.println("ErrorCode: "+ex.getErrorCode());
ex=ex.getNextException();
}
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
try{
if(statement !=null){ statement.close(); }
if(connection !=null){ statement.close(); }
}
catch(SQLException ex){
System.out.println("\nERROR:---- SQLException ----\n");
while(ex!=null){
System.out.println("Message: "+ex.getMessage());
System.out.println("SQLState: "+ex.getSQLState());
System.out.println("ErrorCode: "+ex.getErrorCode());
ex=ex.getNextException();
}
}
}
}
public void closeStmt()
{
try
{
statement.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void closeConn()
{
try
{
connection.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
这是我写的一个连接SQL SERVER2000数据库的JAVABEAN,你把驱动改一下就OK了
包括数据的基本操作
url=jdbc:mysql://localhost/database?useUnicode=true;characterEncoding=gbk
private static boolean nFirstTime=true;
private Connection conn;
try
{
Context initCtx = new javax.naming.InitialContext();
ds =(DataSource)initCtx.lookup(HttpParams.getString(Constants.DATASOURCE));
}
catch (NamingException e)
{
log.error(e.getMessage());
} try
{
conn=ds.getConnection();
}
catch(Exception exp )
{
log.error(exp.getMessage());
}public ResultSet executeQuery(String sql)
{
Statement stmt;
ResultSet rst;
rst = null;
try
{
stmt=conn.createStatement();
rst = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
log.error("Query error:"+sql);
}
return rst;
}public void executeUpdate(String sql) throws SQLException
{
Statement stmt;
ResultSet rst;
try
{
stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
}
catch(SQLException ex)
{
log.error("Update error:"+sql);
throw ex;
}
}