java中swing创建窗口的工具类和连接oracle数据库的工具类

package cn.zz.util;

import java.awt.Window;

import javax.swing.JFrame;

/*

  • 创建一个公共窗口
  • */
    public class BaseFrame
    {
    public static void createBaseFrame(Window window, int width, int height) {
    window.setSize(width, height);
    FrameUtil.frameLocationWithWindow(window);
    ((JFrame) window).setResizable(false);
    ((JFrame) window).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }

package cn.zz.util;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;

public class FrameUtil
{
public static void frameLocationWithWindow(Window window) {
// 获取屏幕的宽和高
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;

	// System.out.println("屏幕的宽:" + screenWidth); // 1440
	// System.out.println("屏幕的高:" + screenHeight); // 900

	// 计算坐标
	int x = (screenWidth - window.getWidth()) / 2;
	int y = (screenHeight - window.getHeight()) / 2;

	// 设置坐标
	window.setLocation(x, y);
}

}

package cn.zz.util;

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

/*

  • 数据库管理工具类:

  • 1.连接数据库

  • 2.关闭数据库

  • */
    public class JDBCManager
    {
    private static final String DRIVER = “oracle.jdbc.driver.OracleDriver”;
    private static final String URL = “jdbc:oracle:thin:@localhost:1521:orcl”;
    private static final String USER = “scott”;
    private static final String PASSWORD = “tiger”;

    private static Connection conn = null;

    // 加载驱动
    static {
    try {
    Class.forName(DRIVER);
    System.out.println(“驱动加载成功!”);
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    // 获取连接
    public static Connection getConnection() {
    try {
    conn = DriverManager.getConnection(URL, USER, PASSWORD);
    System.out.println(“连接成功:” + conn);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return conn;
    }

    // 关闭数据库
    public static void close(Connection conn, Statement stmt, ResultSet rs) {
    try {
    if (rs != null)
    rs.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    if (stmt != null)
    stmt.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    if (conn != null)
    conn.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    public static void close(Connection conn, PreparedStatement stmt, ResultSet rs) {
    try {
    if (rs != null)
    rs.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    if (stmt != null)
    stmt.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    if (conn != null)
    conn.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值