当前位置: 首页 > 工具软件 > DBConnection > 使用案例 >

JDBC编程工具类 Dbconnection

呼延修然
2023-12-01

JDBC基础:https://blog.csdn.net/weixin_44893902/article/details/106746880

 

Dbconnection工具类(包含了连接,增删改查,关闭)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import javax.swing.JOptionPane;
 
import com.mysql.jdbc.Statement;
 
public class DbConnection {
	//驱动类的类名
	private static final String DRIVERNAME="com.mysql.jdbc.Driver";
	//连接数据的URL路径
	private static final String URL="jdbc:mysql://127.0.0.1:3306/moviedb";
	//数据库登录账号
	private static final String USER="root";
	//数据库登录密码
	private static final String PASSWORD="root123";
	//加载驱动
	static{
		try {
			Class.forName(DRIVERNAME);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
    //获取数据库连接
	public static Connection getConnection() {
	          try {
		return DriverManager.getConnection(URL,USER,PASSWORD);
	           } catch (SQLException e) {	
		e.printStackTrace();
	           }
			return null;
	}
	//查询
	public static ResultSet query(String sql) {
		System.out.println(sql);
		//获取连接
		Connection connection=getConnection();
		PreparedStatement psd;
		try {
			psd = connection.prepareStatement(sql);
			return psd.executeQuery();
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null,"执行语句出错\n"+e.toString());
			e.printStackTrace();
		}
		return null;
	}
	//增、删、改、查
		public static int updataInfo(String sql) {
			System.out.println(sql);
			//获取连接
			Connection connection=getConnection();
			try {
				PreparedStatement psd=connection.prepareStatement(sql);
				return psd.executeUpdate();
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,"执行语句出错\n"+e.toString());
				e.printStackTrace();
			}
			return 0;
		}
	//关闭连接
	public static void colse(ResultSet rs,Statement stmt,Connection  conn) throws Exception{
		try { 
			if (rs != null){
				rs.close(); 
			}
            if (stmt != null){
            	stmt.cancel(); 
            }
		 if (conn != null) { conn.close(); }
	   } catch (Exception e) {
		   e.printStackTrace(); throw new Exception();
	   }
	}
}
 类似资料: