⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbconnection.java

📁 jsp+Struts1.2+Hibernate3.1+mysql
💻 JAVA
字号:
package bbs.db;

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

public class DBConnection {

	public static Connection getConnection() {
		Connection con = null;
		String CLASSFORNAME = "org.gjt.mm.mysql.Driver";
		String SERVANDDB = "jdbc:mysql://localhost:3306/test";
		String USER = "root";
		String PWD = "root";

		try {
			Class.forName(CLASSFORNAME);
			con = DriverManager.getConnection(SERVANDDB, USER, PWD);
			con.setAutoCommit(true);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return con;
	}

	public static Statement getStatement() {
		Connection con = DBConnection.getConnection();
		Statement statement = null;
		try {
			statement = con.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return statement;

	}

	public static Statement getStatement(int TYPE_SCROLL_INSENSITIVE,
			int CONCUR_READ_ONLY) {

		Connection con = DBConnection.getConnection();
		Statement statement = null;
		try {
			statement = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return statement;
	}

	public ResultSet query(String sql) {
		ResultSet rs = null;
		Statement stmt = DBConnection.getStatement(
				ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		try {
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	public int update(String sql) {
		int result = 0;
		try {
			Statement stmt = DBConnection.getStatement();
			result = stmt.executeUpdate(sql);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}

⌨️ 快捷键说明

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