dbmade.java

来自「Java购物车及JSTL的应用」· Java 代码 · 共 93 行

JAVA
93
字号
package org.qhit.li.store.dbmade;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBMade {

	/**
	 * 连接数据库
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Connection getCon() throws Exception {
		Connection con = null;
		Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
		con = DriverManager
				.getConnection(
						"jdbc:microsoft:sqlserver://localhost:1433;databasename=Store",
						"sa", "");
		return con;
	}

	/**
	 * getStatement
	 * 
	 * @param con
	 * @return
	 * @throws Exception
	 */
	public static Statement getStm(Connection con) throws Exception {
		Statement stm = null;
		stm = con.createStatement();
		return stm;
	}

	/**
	 * getPreparedStatement
	 * 
	 * @param con
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public static PreparedStatement getPds(Connection con, String sql)
			throws Exception {
		PreparedStatement pds = null;
		pds = con.prepareStatement(sql);
		return pds;
	}

	/**
	 * 数据集
	 * 
	 * @param stm
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public static ResultSet getRs(Statement stm, String sql) throws Exception {
		ResultSet rs = null;
		rs = stm.executeQuery(sql);
		return rs;
	}

	/**
	 * 关闭
	 * 
	 * @param rs
	 * @param stm
	 * @param con
	 */
	public static void close(ResultSet rs, Statement stm, Connection con) {
		try {
			if (rs != null) {
				rs.close();
			}
			if (stm != null) {
				stm.close();
			}
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?