不需要任何框架和开发工具,怎么连接数据库
解决方案 »
- 用命令提示符用fop运行xsl-fo代码总是出现not find org.apache.fop.cli.Main.
- struts1.3+hibernate3.0+spring3.0做查询怎么报错,急
- web工程和一般工程生成的文件有什么不同啊
- ssh问题Servlet action is not available
- 急! 请高手帮助! 用同一个方法解析不同的xml文件!
- eclipse3.2里,spring怎么配啊?
- Weblogic 8.1 sp2英文版使用Ajaxanywhere不刷新,如何解决?
- dynamic-update 的问题
- 〔急〕分数不够再加!Javamail 发送不出去问题
- 请教简单的JAVA语句(extends Applet implements ActionListener)
- 求大家帮帮忙。看看这个表单怎么样模拟提交(跪求啦!!!)
- 请帮忙看这个错误到底处在哪儿?关闭数据库时出错
JDBC?小的不才,沒看明白
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class ConnectionManager { /**
* 返回连接
*
* @return Connection
*/
public static synchronized Connection getConnection() {
// 读出配置信息
String driverClassName = Env.getInstance().getProperty("driver");
String url = Env.getInstance().getProperty("url");
String password = Env.getInstance().getProperty("password");
String user = Env.getInstance().getProperty("user");
Connection con = null;
try {
// 加载数据库驱动程序
Class.forName(driverClassName);
con = DriverManager.getConnection(url,user,password);
} catch (Exception ex) {
ex.printStackTrace();
}
return con;
} /**
* 关闭连接
*
* @param con
*/
public static void closeConnection(Connection con) {
try {
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException ex1) {
System.err.println("Could not close connection : "
+ ex1.getMessage());
}
} /**
* 关闭结果集
*
* @param res
* ResultSet
*/
public static void closeResultSet(ResultSet res) {
try {
if (res != null) {
res.close();
res = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 关闭语句
*
* @param pStatement
* PreparedStatement
*/ public static void closeStatement(PreparedStatement pStatement) {
try {
if (pStatement != null) {
pStatement.close();
pStatement = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 关闭Statement
*/
public static void closeStatement(Statement state) {
try {
if (state != null) {
state.close();
state = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
配置文件名为:db.properties的信息
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433;DatabaseName=bookInfo
user=sa
password=123456获取配置文件信息类import java.io.InputStream;
import java.util.Properties;public final class Env extends Properties { private static final long serialVersionUID = 1L; private static Env instance; /**
* 公有的获取实例的方法
* @return Env 实例
*/
public static Env getInstance() {
if (instance != null) {
return instance;
} else {
makeInstance();
return instance;
}
} /**
* 同步的创建实例方法
*
*/
private static synchronized void makeInstance() {
if (instance == null) {
instance = new Env();
}
}
/**
* 私有的构造方法,确保实例的唯一性
*
*/
private Env() {
InputStream is = getClass().getResourceAsStream("/db.properties");
try {
load(is);
} catch (Exception e) {
System.err.println("错误:没有读取属性文件,请确认db.property文件是否存在。");
return;
}
}
}
通用操作类(增删改查)import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;import javax.servlet.jsp.jstl.sql.*;public class SqlCommandBean { private Connection conn = null;// 连接数据库对象
private List sqlValue;// sql语句的参数
private String sql;// 执行的sql语句 // 设置参数
public void setSqlValue(List sqlValue) {
this.sqlValue = sqlValue;
} // 设置sql语句
public void setSql(String sql) {
this.sql = sql;
}
//设置数据库连接
public void setConn(Connection conn) {
this.conn = conn;
}
/**
* 定义语句的参数
*
* @throws Exception
*/
private void setParameter(PreparedStatement preStatement) throws Exception {
for (int i = 0; i < sqlValue.size(); i++) {
preStatement.setObject(i + 1, sqlValue.get(i));
}
} /**
* 执行Update,Insert,Delete语句
*/
public int executeUpdate() {
int result = 0;
PreparedStatement preStatement = null;
Statement state = null;
try {
// 判断是否带有参数
if (sqlValue != null && sqlValue.size() > 0) {
preStatement = conn.prepareStatement(sql);
setParameter(preStatement);// 设置参数
result = preStatement.executeUpdate();
} else {
state = conn.createStatement();
result = state.executeUpdate(sql);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (preStatement != null) {
ConnectionManager.closeStatement(preStatement);
}
if (state != null) {
ConnectionManager.closeStatement(state);
}
if (conn != null) {
ConnectionManager.closeConnection(conn);
}
}
return result;
} /**
* 执行数据库中的查询语句
*/
public Result executeQuery() {
Result result = null;
ResultSet rs = null;
PreparedStatement preStatement = null;
Statement state = null;
try {
// 判断是否带有参数
if (sqlValue != null && sqlValue.size() > 0) {
preStatement = conn.prepareStatement(sql);
setParameter(preStatement);// 设置参数
rs = preStatement.executeQuery();
} else {
state = conn.createStatement();
rs = state.executeQuery(sql);
}
result = ResultSupport.toResult(rs); } catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
ConnectionManager.closeResultSet(rs);
}
if (preStatement != null) {
ConnectionManager.closeStatement(preStatement);
}
if (state != null) {
ConnectionManager.closeStatement(state);
}
if (conn != null) {
ConnectionManager.closeConnection(conn);
}
}
return result;
}
}
2 建立连接(地址,用户名,密码之类的)
3 设置SQL
4 运行SQL,可能包括COMMIT,ROLLBACK操作
5 关闭连接