mysql传入Bean_连接MySQL数据库生成JavaBean

做网站时,感觉数据库的设计比较重要,当花了很多时间设计好数据库时,就希望有一个能自动生成bean的工具,虽然Eclipse的插件能反向生成bean和hibernate的配置文件,但总感觉不够灵活,不够小到随意简单使用。但又实在不想去敲代码写一个个很多属性的bean。网上没找着现成合适的,于是就花了点时间自己写了个生成工具玩玩,也希望能给需要的朋友们一点点帮助~~

直接放出可执行的exe程序和源码了,喜欢且需要的朋友尽管拿去用。

3a104f2d74d9e3a0f4cb50a66b3f9287.png

MySQLToBean.java

package org.just.util;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Properties;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import javax.swing.JFrame;

import javax.swing.JPanel;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JTextField;

import javax.swing.JLabel;

import javax.swing.JCheckBox;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

import java.awt.Color;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

/**

* 此类用来将mysql的表直接生成Bean

*

* @author [email protected]

*/

public class MySQLToBean extends JFrame {

/**

*

*/

private static final long serialVersionUID = 1L;

private JCheckBox checkBox;

Properties p = new Properties();

String configFile = "config.ini";

private JLabel lblNewLabel_4;

public MySQLToBean() {

setResizable(false);

setTitle("MySQL生成javabean小工具");

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

setBounds(100, 100, 484, 324);

JPanel panel = new JPanel();

getContentPane().add(panel, BorderLayout.CENTER);

panel.setLayout(null);

txtLocalhost = new JTextField();

txtLocalhost.setText("localhost");

txtLocalhost.setBounds(146, 10, 147, 21);

panel.add(txtLocalhost);

txtLocalhost.setColumns(10);

JLabel lblIp = new JLabel("IP:");

lblIp.setBounds(80, 13, 30, 15);

panel.add(lblIp);

JLabel label = new JLabel("数据库:");

label.setBounds(80, 42, 54, 15);

panel.add(label);

textField = new JTextField();

textField.setBounds(146, 39, 147, 21);

panel.add(textField);

textField.setColumns(10);

JLabel label_1 = new JLabel("表名:");

label_1.setBounds(80, 127, 54, 15);

panel.add(label_1);

textField_1 = new JTextField();

textField_1.setBounds(146, 124, 147, 21);

panel.add(textField_1);

textField_1.setColumns(10);

JLabel label_2 = new JLabel("包名:");

label_2.setBounds(79, 156, 54, 15);

panel.add(label_2);

txtComyourcom = new JTextField();

txtComyourcom.setText("com.yourcom.bean");

txtComyourcom.setBounds(146, 155, 147, 21);

panel.add(txtComyourcom);

txtComyourcom.setColumns(10);

JLabel lblNewLabel = new JLabel("输出目录:");

lblNewLabel.setBounds(80, 190, 65, 15);

panel.add(lblNewLabel);

textField_3 = new JTextField();

textField_3.setBounds(146, 186, 147, 21);

panel.add(textField_3);

textField_3.setColumns(10);

checkBox = new JCheckBox("生成包结构目录");

checkBox.setSelected(true);

checkBox.setBounds(145, 213, 147, 23);

panel.add(checkBox);

JLabel lblNewLabel_1 = new JLabel("可以指定表名,也可以不指定");

lblNewLabel_1.setBounds(303, 127, 176, 15);

panel.add(lblNewLabel_1);

JLabel lblNewLabel_2 = new JLabel("* 数据库名");

lblNewLabel_2.setForeground(Color.RED);

lblNewLabel_2.setBounds(303, 42, 66, 15);

panel.add(lblNewLabel_2);

JLabel lblNewLabel_3 = new JLabel("* 包结构");

lblNewLabel_3.setForeground(Color.RED);

lblNewLabel_3.setBounds(303, 158, 79, 15);

panel.add(lblNewLabel_3);

JButton button = new JButton("执行");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

go();

}

});

button.setBounds(145, 242, 93, 23);

panel.add(button);

textField_4 = new JTextField();

textField_4.setText("123456");

textField_4.setBounds(145, 93, 147, 21);

panel.add(textField_4);

textField_4.setColumns(10);

txtRoot = new JTextField();

txtRoot.setText("root");

txtRoot.setBounds(14

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaBean连接MySQL数据库的步骤如下: 1. 导入MySQL的JDBC驱动包。 2. 在JavaBean中使用JDBC API连接MySQL数据库。 3. 创建一个Connection对象,用于连接MySQL数据库。 4. 创建一个Statement对象,用于执行SQL语句。 5. 执行SQL语句,并获取结果集。 6. 处理结果集,将数据存储到JavaBean中。 7. 关闭结果集、Statement对象和Connection对象。 具体实现步骤可以参考以下代码: ``` import java.sql.*; public class JavaBean { private String url = "jdbc:mysql://localhost:3306/test"; private String user = "root"; private String password = "123456"; private Connection conn = null; private Statement stmt = null; private ResultSet rs = null; public void connect() { try { // 加载MySQL的JDBC驱动 Class.forName("com.mysql.jdbc.Driver"); // 创建连接 conn = DriverManager.getConnection(url, user, password); // 创建Statement对象 stmt = conn.createStatement(); // 执行SQL语句 String sql = "SELECT * FROM user"; rs = stmt.executeQuery(sql); // 处理结果集 while (rs.next()) { String name = rs.getString("name"); int age = rs.getInt("age"); System.out.println("name: " + name + ", age: " + age); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭结果集、Statement对象和Connection对象 try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值