当前位置:数据库 > MySQL >>

谁能给个ssh中 mysql的dao类代码 数据库部分

什么 Class.forName("com.mysql.jdbc.Driver").什么的 。。。 用别的dao类可以继承这个类,从而访问数据库,急啊 ,谢了大神们。。。
补充:说明白了 就是 java链接mysql数据库 的basedao
追问:就是以前复制粘贴惯了。。硬盘丢了。。记忆很模糊 就是basedao忘了 倒是会增删改查什么滴~
以前都是用一个类继承basedao,然后才和数据库交互。。现在 把“老大”basedao忘了啊
答案:DB接口:
package org.xxx.dbc;

import java.sql.Connection;

public interface IDatabaseConnection {
	public Connection getConnection() throws Exception;
	
	public void close() throws Exception;
}
------------------------------------------------------------------
实现:
package org.xxx.dbc.impl;

import java.sql.Connection;
import java.sql.DriverManager;

import org.xxx.dbc.IDatabaseConnection;

public class OracleDatabaseConnection implements IDatabaseConnection {
	public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
	public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:db1";	public static final String DBUSER = "name";
	public static final String DBPASS = "pass";

	private Connection conn = null;

	@Override
	public void close() throws Exception {
		// TODO Auto-generated method stub
		try {
			if (this.conn != null) {
				this.conn.close();
			}
		} catch (Exception e) {
			throw e;
		}

	}
	@Override
	public Connection getConnection() throws Exception {
		// TODO Auto-generated method stub
		try {
			Class.forName(DBDRIVER);
			this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);

		} catch (Exception e) {
			throw e;
		}
		return this.conn;
	}

}
----------------------------------------------------------------------------------
工厂
package org.xxx.dbc.factory;

import org.xxx.dbc.IDatabaseConnection;
import org.xxx.dbc.impl.OracleDatabaseConnection;

public class DBCFactory {
	public static IDatabaseConnection getOracleDatabaseConnection()throws Exception {
		return new OracleDatabaseConnection();
	}
} 

看错了,以为是oracle .不影响使用
其他:汗.....java链接mysql就七步啊...和链接sqlserver一样. 。。。SSH整合为什么要用JDBC ?自带的数据库连接池不是更好吗? Class.forName("com.mysql.jdbc.Driver").这是jdbc连接数据库…… 真正的dao是在分层里的 

上一个:mysql数据库load data使用问题
下一个:mysql 1064 错误 以前网站好好的 结果昨天刚装了个 PHPWIND论坛程序 结果弄的网站出错。错误如下

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,