java在数据库插入bean

创建数据库表:

create table test.Student(
    ID VARCHAR(10),
    Name VARCHAR(20),
    Bedroom VARCHAR(20),
    Zhuanye VARCHAR(40),
    Sex VARCHAR(1)
  )
/* bean 代码 */

package student;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class stuBean extends JPanel {
  private static final long serialVersionUID = 9166303709249041951L;
  private JComboBox combBed;
  private JComboBox combDanyuan;
  private JComboBox combFool;
  private JTextField textName;
  private JTextField textID;
  private JComboBox combSex;
  private JComboBox combSpecial;
  private String bedroom;
  private String special;
  private String sex;
  private String userName="root";
  private String password="";
  private String url;
  private String strDriver;
  private Connection conn;
  private java.sql.PreparedStatement stat;
  /**
   * Create the panel
   */
  public stuBean() {
    super();
    setLayout(null);
    final JLa
bel label = new JLabel();
    label.setText("学号");
    label.setBounds(37, 29, 66, 18);
    add(label);
    textID = new JTextField();
    textID.setBounds(109, 27, 119, 22);
    add(textID);
    final JLabel label_1 = new JLabel();
    label_1.setText("姓名");
    label_1.setBounds(37, 79, 66, 18);
    add(label_1);
    textName = new JTextField();
    textName.setBounds(109, 77, 119, 22);
    add(textName);
    final JLabel label_2 = new JLabel();
    label_2.setText("宿舍");
    label_2.setBounds(37, 129, 66, 18);
    add(label_2);
    combFool = new JComboBox();
    combFool.setBounds(109, 125, 43, 27);
    combFool.addItem(new String("1"));
    combFool.addItem(new String("2"));
    combFool.addItem(new String("3"));
    combFool.addItem(new String("4"));
    combFool.addItem(new String("5"));
    combFool.addItem(new String("6"));
    combFool.addItem(new String("7"));
    combFool.addItem(new String("8"));
    combFool.addItem(new style="COLOR: #ff0000">String("9"));
    combFool.addItem(new String("10"));
    add(combFool);
    final JLabel label_3 = new JLabel();
    label_3.setText("楼");
    label_3.setBounds(158, 129, 26, 18);
    add(label_3);
    combDanyuan = new JComboBox();
    combDanyuan.setBounds(200, 125, 43, 27);
    combDanyuan.addItem(new String("1"));
    combDanyuan.addItem(new String("2"));
    combDanyuan.addItem(new String("3"));
    add(combDanyuan);
    final JLabel label_4 = new JLabel();
    label_4.setText("单元");
    label_4.setBounds(253, 129, 31, 18);
    add(label_4);
    combBed = new JComboBox();
    combBed.setBounds(290, 125, 43, 27);
    combBed.addItem(new String("1"));
    combBed.addItem(new String("2"));
    combBed.addItem(new String("3"));
    combBed.addItem(new String("4"));
    combBed.addItem(new String("5"));
    combBed.addItem(new String("6"));
    combBed.addItem(new String("7"));
    combBed.addItem(new String("8"));
    add(co
mbBed);
    final JLabel label_5 = new JLabel();
    label_5.setText("床");
    label_5.setBounds(339, 129, 53, 18);
    add(label_5);
    final JLabel label_6 = new JLabel();
    label_6.setText("专业");
    label_6.setBounds(37, 184, 66, 18);
    add(label_6);
    combSpecial = new JComboBox();
    combSpecial.setBounds(109, 180, 147, 27);
    combSpecial.addItem(new String("linux 软件开发方向"));
    combSpecial.addItem(new String("java 软件开发方向"));
    combSpecial.addItem(new String("嵌入软件开发方向"));
    combSpecial.addItem(new String("对日软件开发方向"));
    combSpecial.addItem(new String(".net 软件开发方向"));
    add(combSpecial);
    combSex = new JComboBox();
    combSex.setBounds(109, 232, 53, 27);
    combSex.addItem(new String("男"));
    combSex.addItem(new String("女"));
    add(combSex);
    final JLabel label_7 = new JLabel();
    label_7.setText("性别");
    label_7.setBounds(37, 236, 66, 18);
    add(label_7);
    final JButton button = new JButton();
    button.addActionListener(new ActionListener() {
      public void actionPerformed(final ActionEvent e0000cc">) {
        bedroom = combFool.getSelectedItem().toString()
            + combDanyuan.getSelectedItem().toString()
            + combBed.getSelectedItem().toString();
        special = combSpecial.getSelectedItem().toString();
        if (combSex.getSelectedIndex() == 0) {
          sex = "m";
        } else {
          sex = "f";
        }
        dbConnection();
        dbUpdate();
        dbClose();
      }
    });
    button.setText("提交");
    button.setBounds(121, 303, 100, 28);
    add(button);
    //
  }
  public void dbConnection() {
    try {
      Class.forName(strDriver);
      conn = DriverManager.getConnection(url, userName, password);
    } catch (Exception e) {
      e.printStackTrace();
      JOptionPane.showMessageDialog(null, "连接数据库出错", "error!",
          JOptionPane.ERROR_MESSAGE);
    }
  }
  public void dbUpdate() {
    try {
      stat = conn.prepareStatement("insert into test.Student(Id,Name,BedRoom,Zhuanye,Sex) values(?,?,?,?,?)");
      stat.setString(1, textID.getText());
      stat.setString(2, textName.getText()
stat.setString(3, bedroom);
      stat.setString(4, special);
      stat.setString(5, sex);
      int i = stat.executeUpdate();
      if (i == 1) {
        JOptionPane.showMessageDialog(this, "成功插入一行数据", "sucess",
            JOptionPane.PLAIN_MESSAGE);
      } else {
        JOptionPane.showMessageDialog(this, "提交出错", "error",
            JOptionPane.ERROR_MESSAGE);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
  public void dbClose() {
    try {
      stat.close();
      conn.close();
    } catch (SQLException e) {
      e.printStackTrace();
      JOptionPane.showMessageDialog(null, "关闭数据库出错", "error!",
          JOptionPane.ERROR_MESSAGE);
    }
  }
  public String getPassword() {
    return password;
  }
  public String getStrDriver() {
    return strDriver;
  }
  public String getUrl() {
    return url;
  }
  public String getUserName() {
    return userName;
  }
  public void setPassword(String password) {
    this.password = password;
  }
  public void setStrDriver(String strDriver) {
    this.strDriver = strDriver;
  }
  public void setUrl(String url) {
    this.url = url;
  }
  public void setUserName(String userName) {
    this.userName = userName;
  }
}

时间: 2024-09-22 23:30:07

java在数据库插入bean的相关文章

线程-java读取文件插入到数据库程序

问题描述 java读取文件插入到数据库程序 在OEPE上开发一个java项目,实现2个线程同时循环往Oracle中插入staff.txt中读到的数据,表结构随意,id自增,两线程无限循环,不使用框架,直接在Junit启动. 解决方案 java实现读取XML文件数据插入到数据库中 解决方案二: 首先,OEPE是什么东西呢?这个文件由两个线程同时读取吗?这个可以是一个建单的应用,定义一个线程类,其run主要就是解析文件的内容插入到数据库.然后在main函数中同时启动两个线程,处理文件内容.

java jdbc连接mysql数据库插入null值问题

问题描述 java jdbc连接mysql数据库插入null值问题 ps.setFloat(8 device.getLatitude());如果device.getLatitude()返回的是null就报错,也就是说setFloat不能插入空值,而setString()方法就可以,不知道为什么请高手指点,谢谢!! 解决方案 PreparedStatement.java 的给方法签名是这样的public void setFloat(int parameterIndex float x) throw

java-关于用jdbc向数据库插入date字段为空值的 为什么 会报错

问题描述 关于用jdbc向数据库插入date字段为空值的 为什么 会报错 这是抛出来的异常 求大神救命..! java.sql.SQLException: 无效的列类型 at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134) at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179) at oracle.jdbc.dbaccess.DBErr

编码-java向mySql插入数据乱码

问题描述 java向mySql插入数据乱码 为什么我执行以下命令行后暂时成功改成了utf8,但是退出mysql后重新进入又变成gbk编码了呢 mysql> SET character_set_client = utf8; mysql> SET character_set_results = utf8; mysql> SET character_set_connection = utf8; 解决方案 你要在my.ini需要修改两处 default-character-set=utf8 ch

JDBC 向数据库插入数据 遇到个怪问题 求大神回答

问题描述 JDBC 向数据库插入数据 遇到个怪问题 求大神回答 代码如下package insertCar; import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List; public

java实现oracle插入当前时间的方法_java

本文实例讲述了java实现oracle插入当前时间的方法.分享给大家供大家参考.具体分析如下: 我在做一个inser操作的时候 要获取当前时间 存入数据库 就直接 ps.setDate(new java.util.Date()); 但是会报转换错误 此时应该 java.util.Date utilDate = new java.util.Date(); java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); ps.setDat

Java编程 数据库MySQL 添加数据

问题描述 Java编程 数据库MySQL 添加数据 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run

servlet-不知道那错了,jsp向数据库插入操作

问题描述 不知道那错了,jsp向数据库插入操作 50C 这样用Demo.java能成功而在demo.jsp用同样的方式,不报错,但是没插入成功.求详解 解决方案 Jsp中没有createConnectio()仔细看看它和java中的区别.另外,用完后最好把连接都关了 解决方案二: 插入字符串(不知道哪里错了)jsp向数据库中插入中文乱码 解决方案三: 能把你的dbBean里的代码贴出来吗

(初学者)eclipse编写java 对数据库的操作问题,不知道问题出在哪里,无法入库

问题描述 (初学者)eclipse编写java 对数据库的操作问题,不知道问题出在哪里,无法入库 代码如下:QueryRunner queryRunner = new QueryRunner(); String sql = ""delete from customers where id = ?""; Connection connection = null; try { connection = JDBCTools.getConnection(); System.