使用GUI java将数据插入MS Access数据库

时间:2015-11-25 12:37:55

标签: java

我想使用Java GUI将数据插入MS访问数据库,但我收到以下错误:

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.2 data exception: invalid character value for cast

以下是我的代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.io.*;
class Record1 extends JFrame implements ActionListener
{
JLabel l1,l2,l3,l4;
JTextField t1,t2,t3,t4;
JButton b1;
public Record1()
{
    setLayout(new FlowLayout());
    l1=new JLabel("Name");  
    l2=new JLabel("Roll_no");       
    l3=new JLabel("Course");        
    l4=new JLabel("Fees");
    t1=new JTextField(20);  
    t2=new JTextField(10);
    t3=new JTextField(10);
    t4=new JTextField(10);
    b1=new JButton("REGISTER");
    add(l1);add(t1);add(l2);add(t2);
    add(l3);add(t3);add(l4);add(t4);add(b1);
    setSize(400,400);   
    setTitle("Registration");
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
    try{
    Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
    Connection con=DriverManager.getConnection("jdbc:ucanaccess://D:/java/jdbc/Database2.mdb");
PreparedStatement psmt=con.prepareStatement("insert into reg values (?,?,?,?)");
        String name,course;
        int rn,fees;
        name= t1.getText();
        rn=Integer.parseInt(t2.getText());
        course=t3.getText();
        fees=Integer.parseInt(t4.getText());
        if(e.getSource()==b1)
    {   
        psmt.setString(1,name);
        psmt.setInt(2,rn);
        psmt.setString(3,course);
        psmt.setInt(4,fees);
        psmt.executeUpdate();
    }
        con.close();
    System.out.println("Success");
    }catch(Exception r){
    System.out.println(r);
            }
}
public static void main(String[] arr)
{
    new Record1();
}
}

0 个答案:

没有答案