java/jsp/javscript问题
我想采用SQLSEVER2000数据库,语言是java/jsp/javscript
准备开发个网站,哪位能提供连接数据库的DB.java类,
将不胜感谢
问题点数:0、回复次数:6Top
1 楼xchfriend(xchfriend)回复于 2003-12-03 08:32:42 得分 0
我用的是jdbc-odbc桥连接数据库的!
package sql;
import java.sql.*;
public class faq
{
String url="jdbc:odbc:faq";//把这里的faq改成你的映象数据库名!
Connection con=null;
Statement stmt;
ResultSet rs=null;
public faq()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url);
stmt=con.createStatement();
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("create():"+e.getMessage());
}
catch(SQLException e)
{
System.err.println("something error");
}
}
public boolean executeUpdate(String sql)
{
try
{
stmt.executeUpdate(sql);
return true;
}
catch(SQLException e)
{
System.err.println("aq.executeUpdate:"+e.getMessage());
}
return false;
}
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
rs=stmt.executeQuery(sql);
}
catch(SQLException e)
{
System.err.println("aq.executeUpdate:"+e.getMessage());
}
return rs;
}
public Statement getStatement()
{
return stmt;
}
public void close()
{
try
{
rs.close();
stmt.close();
con.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}Top
2 楼moulinjie1(l)回复于 2003-12-03 08:51:23 得分 0
我有你能给我邮箱吗?发到moulinjie@tom.com.cnTop
3 楼moulinjie1(l)回复于 2003-12-03 08:54:22 得分 0
我该你发一份Top
4 楼jxnclhjjxnclhj(eee)回复于 2003-12-03 09:24:36 得分 0
非常谢谢各位,我的邮箱是jxnclhj@sina.com.cn
如果有java.db类发给我,我将不胜感谢Top
5 楼jxnclhjjxnclhj(eee)回复于 2003-12-03 10:07:22 得分 0
我用的是xml数据库,如果谁有兴趣,我可以提供。上楼那位朋友的方法我试了,可以操作。
不过,我想表中的每个记录就是一个对象,可以对对象进行操作。例如:返回一个记录集,就是返回一个ArrayList
public static ArrayList getObjectList(String xquery, HashMap params,
Class objClass) throws Exception {
Session session = null;
try {
session = IXClientSessionFactory.getSession(host, port, username,
password);
XQueryPreparedStatement stmt = session.createXQueryPreparedStatement(
xquery);
if (params != null) {
Iterator it = params.keySet().iterator();
while (it.hasNext()) {
String name = (String) it.next();
String value = checkValue( (String) params.get(name));
stmt.setParameter(name, value);
}
}
XQueryResultSet result = stmt.executeQuery();
ArrayList list = new ArrayList();
XQueryResult resultNode = result.next();
while (resultNode != null) {
Node node = resultNode.getNode();
list.add(Translate.XML2Object(objClass, node));
resultNode = result.next();
}
return list;
} finally {
if (session != null) {
session.close();
}
}
}
添加表中一行记录,就是添加一个对象,我用xml数据库时用过,不知在SQLserver中有没有用过。我这里有个添加记录的,是xml数据库
/**
*
* @param collectionName
* @param documentName
* @param xpath
* @param obj
* @return returns an integer count indicating how many nodes/values have
* been updated. If the returned value is 0, it means nothing has been updated
* @throws java.lang.Exception
*/
public static int insertObject(String collectionName, String documentName,
String xpath, Object obj) throws Exception {
Session session = null;
try {
Node node = Translate.Object2XML(obj);
session = IXClientSessionFactory.getSession(host, port, username,
password);
XUpdateStatement stmt = session.createXUpdateStatement();
return stmt.insertBefore(collectionName, documentName, xpath, node);
} finally {
if (session != null) {
session.close();
}
}
}Top
6 楼wind0209(风のFeeling)回复于 2003-12-03 10:33:30 得分 0
ODBC-JDBC连法,结果集存在VECTOR里返回
package feeling;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.sql.*;
import javax.naming.*;
public class Db_conf
{
public Connection conn=null;
public ResultSet rs=null;
public Statement stmt = null;
public Db_conf()
{
//nothing now
}
public void getConn()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:webdb","","");
stmt = conn.createStatement();
}
catch(SQLException e)
{
//do sth.
}
catch(Exception ex)
{
//do sth.
}
}
public void closeConn()
{
try
{
if(conn!=null)conn.close();
}
catch(Exception e)
{
//do sth
}
}
public int eUpdate(String sql)
{
int stats=0;
try
{
stats=stmt.executeUpdate(sql);
}
catch(SQLException e)
{
//do sth.
}
return stats;
}
public Vector eQuery(String sql)
{
Vector tmpV=new Vector();
Vector tmpV2=null;
try
{
rs=stmt.executeQuery(sql);
int rsColCount=rs.getMetaData().getColumnCount();
while(rs.next())
{
tmpV2=new Vector();
for(int i=1;i<=rsColCount;i++)
{
String str=rs.getString(i);
tmpV2.addElement(str);
}
tmpV.addElement(tmpV2);
}
}
catch(SQLException e)
{
//do sth.
}
return tmpV;
}
}Top




