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();
    }
    }
    }

猜你喜欢

转载自blog.csdn.net/qq_43997991/article/details/89462065