Swing练习随笔记录

2023-02-10 13:41:19   第一文档网     [ 字体: ] [ 阅读: ] [ 文档下载 ]

#第一文档网# 导语】以下是®第一文档网的小编为您整理的《Swing练习随笔记录》,欢迎阅读!
随笔,练习,记录,Swing
1.jdbc连接数据库过程

package com.pb.util;

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

public class DbUtil {

String user="postgres"; String passWord="red___";

String url= "jdbc:postgresql://127.0.0.1:5432/bookManger"; String jdbcname="org.postgresql.Driver"; //获取数据库连接

public Connection getCon() throws Exception{ Class.forName(jdbcname);

Connection con=DriverManager.getConnection(url,user,passWord); return con; }

//关闭数据库连接

public void closeCon(Connection con) throws SQLException{ if(con!=null){ con.close(); } } /**

* @param args */

public static void main(String[] args) { DbUtil db=new DbUtil();

// TODO Auto-generated method stub try {

db.getCon();

System.out.println("数据库连接成功!"); } catch (Exception e) {

// TODO Auto-generated catch block e.printStackTrace(); } } }


2.Swing常用代码 关闭一个窗口

this.dispose(); 设置窗口全屏显示

this.setExtendedState(JFrame.MAXIMIZED_BOTH);

跳转到另一个窗口

new MainFrm().setVisible(true);

窗口提示错误信息

JOptionPane.showMessageDialog(null, "用户名或密码错误!");



新打开的JinternetFram窗口调整位置

this.setLocation(x, y);

查询数据库中的数据显示在前台的表中---重要的两步 1dao层里

public ResultSet bookTypeList(Connection con) throws SQLException{ String sql="select * from addbooktype_table"; PreparedStatement ps=con.prepareStatement(sql); return ps.executeQuery(); }

2jInternetFram

public void fillTable(){

DefaultTableModel dtm=(DefaultTableModel) bookTypeTable.getModel(); Connection con=null; try {

con=db.getCon();

ResultSet rs=bookTypeDao.bookTypeList(con); while(rs.next()){

Vector v=new Vector();

v.add(rs.getString("bookType")); v.add(rs.getString("typedes")); dtm.addRow(v); }

} catch (Exception e) {

// TODO Auto-generated catch block e.printStackTrace(); }finally{ try {

db.closeCon(con);

} catch (SQLException e) {









// TODO Auto-generated catch block e.printStackTrace();


本文来源:https://www.dy1993.cn/rS8K.html

相关推荐