java的JDBC连接数据库Mysql,sqlserver, Oracle dao模式
public class BaseDao {
String DBUser = "root"; // 用户名
String DBPwd = "root";// 密码
String DBHost = "localhost"; // 服务器地址
String DBName = "myschool";// 数据库名称
int Port = 3306; // 端口号
int DBType = 1; // 1mysql,2sqlserver,3orcale
Connection conn; // 连接对象
PreparedStatement ps; // 查询对象
ResultSet rs; // 结果集
/**
* 打开连接
*/
public void open() {
try {
if (DBType == 1) {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://" + DBHost
+ ":" + Port + "/" + DBName, DBUser, DBPwd);
}
else if (DBType == 2) {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager
.getConnection("jdbc:sqlserver://" + DBHost + ":"
+ Port + ";databasename=" + DBName, DBUser,
DBPwd);
} else if (DBType == 3) {
// 编写orcala
Class.forName("Oracle.jdbc.driver.OracleDriver");
conn = DriverManager
.getConnection("jdbc:oracle:thin:@" + DBHost + ":"
+ Port + ":" + DBName, DBUser,
DBPwd);
}
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* 关闭连接
*/
public void close() {
try {
if (rs != null)
rs.close(); // 如果结果集存在,就关闭结果集
ps.close(); // 关闭查询对象
conn.close(); // 关闭连接查询
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* 执行 增删
* @param sql
* @param parm
* @return a 为受影响的行数
*/
public int exectueUpdate(String sql, Object[] parm) {
open();
int a = 0;
try {
ps = conn.prepareStatement(sql);
if (parm != null) {
for (int i = 0; i < parm.length; i++) {
ps.setObject(i + 1, parm[i]);
}
}
a = ps.executeUpdate(); // 执行
} catch (Exception e) {
// TODO: handle exception
}
close();
return a;
}
/**
* 执行查询
* @param sql
* @param parm
* @return
*/
public ResultSet exectueQuery(String sql,Object[] parm){
open();
try {
ps = conn.prepareStatement(sql);
if (parm != null) {
for (int i = 0; i < parm.length; i++) {
ps.setObject(i + 1, parm[i]);
}
}
rs=ps.executeQuery();
} catch (Exception e) {
// TODO: handle exception
}
return rs;
}
}
使用继承或者new也可以
package com.xing.myschool.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import com.xing.myschool.entity.Student;
public class StudentDao extends BaseDao {
public int add(Student stu) {
String sql = "insert into student values(?,?,?,?)";
Object[] parm = { stu.getStudentid(), stu.getStudentName(),
stu.getStudentSex(), stu.getStudentAge() };
int a = exectueUpdate(sql, parm);
return a;
}
public int delete(int studentId) {
String sql = "delete from student where Id=? ";
Object[] parm = { studentId };
int a = exectueUpdate(sql, parm);
return a;
}
public int update(Student stu) {
String sql = "update Student set name=?, age=?,sex=? where Id=?;";
Object[] parm = { stu.getStudentName(), stu.getStudentAge(),
stu.getStudentSex(), stu.getStudentid() };
int a = exectueUpdate(sql, parm);
return a;
}
public ArrayList<Student> getall() {
String sql = "select * from student";
ArrayList<Student> list = new ArrayList<Student>();
rs = exectueQuery(sql, null);
try {
while (rs.next()) {
Student stuu = new Student();
stuu.setStudentid(rs.getInt("id"));
stuu.setStudentName(rs.getString("name"));
stuu.setStudentSex(rs.getString("sex"));
stuu.setStudentAge(rs.getInt("age"));
list.add(stuu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
close();
return list;
}
}
所有的代码地址:点击打开链接
使用的软件为:MyEclipse