package TechBook;
import java.awt.*;
import java.awt.event.*;import javax.swing.*;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.sql.*;
public class ZB25IN extends JFrame implements ActionListener{
/*
 * SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentTime = new Date();//得到当前系统时间
String str_date1 = formatter.format(currentTime); 
    String u=user.getText();
    jt.setText(u+"\n"+str_date1);
 */
JLabel barcode=new JLabel("条形码");
JTextField getbarcode=new JTextField(16);
JLabel machine_part_code=new JLabel("零件编码");
JTextField getmachine_part_code=new JTextField(16);
JLabel papername=new JLabel("图纸名称");
JTextField getpapername =new JTextField(16);
JLabel partcode =new JLabel("零件编码");
JTextField getpartcode=new JTextField(16);
JLabel partbom=new JLabel("图号");
JTextField getpartbom=new JTextField(16);
JLabel pici=new JLabel("批次");
JTextField getpici =new JTextField(10);
JLabel kucode=new JLabel("库号");
JTextField getkucode=new JTextField(16);
JLabel weicode =new JLabel("位号");
JTextField getweicode=new JTextField(16);
JLabel inkutime =new JLabel("入库时间");
//SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
JTextField getinkutime=new JTextField(16);
//JTextField getinkutime=new JTextField(16);
//JTextField getinkutime=new JTextField(16);
//Machine_part_code Name Part_bom Barcode Pici intoKu_time outKu_time
JButton add=new JButton("添加");
JButton delete=new JButton("删除");
JButton reset=new JButton("重置");
JButton out=new JButton("退出");
JPanel px=new JPanel();
JPanel pd=new JPanel();
JPanel pb=new JPanel();
JPanel pn=new JPanel();
JPanel pmain=(JPanel)getContentPane();

public ZB25IN(){

setTitle("图纸入库");
setSize(700,300);
setLocation(100,150);
px.setLayout(new GridLayout(5,4,10,10));
px.add(barcode);
px.add(getbarcode);
px.add(machine_part_code);
px.add(getmachine_part_code);
px.add(papername);
px.add(getpapername);
px.add(partcode);
px.add(getpartcode);
px.add(partbom);
px.add(getpartbom);
px.add(pici);
px.add(getpici);
px.add(kucode);
px.add(getkucode);
px.add(weicode);
px.add(getweicode);
px.add(inkutime);
px.add(getinkutime);
pb.setLayout(new GridLayout(1,5,10,10));
        pb.add(add);
    pb.add(delete);
    pb.add(reset);
    pb.add(out);
    pn.setLayout(new GridLayout(2,1,10,10));
    pn.add(pd);
    pn.add(pb);
    pmain.setLayout(new BorderLayout());
    pmain.add(px,BorderLayout.NORTH);
    pmain.add(pn,BorderLayout.SOUTH);
    add.addActionListener(this);
    delete.addActionListener(this);
    reset.addActionListener(this);
    out.addActionListener(this);
     }
/*public void mouseEntered(MouseEvent e){
String s = "";
getinkutime.setText(s);;
}*/
public void actionPerformed(ActionEvent ex){
/*SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentTime = new Date();//得到当前系统时间
String str_date1 = formatter.format(currentTime); 
    String u=user.getText();
    jt.setText(u+"\n"+str_date1);*/
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentTime=new Date();
String str=formatter.format(currentTime);
getinkutime.setText(str);

if(ex.getSource()==add){

if(getpapername.getText().equals("")||getbarcode.getText().equals("")||getmachine_part_code.getText().equals("")||getpartcode.getText().equals("")||getpartbom.getText().equals("")||getpici.getText().equals("")||getkucode.getText().equals("")||getweicode.getText().equals("")){
JOptionPane.showMessageDialog(null,"请将图纸信息填完整!");
}
else{
DBruku.addbook(getpapername.getText(),getbarcode.getText(),getmachine_part_code.getText(),getpartcode.getText(),getpici.getText());
JOptionPane.showMessageDialog(null,"入库成功!");
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;DatabaseName=drawing paper";
//Connection con=DriverManager.getConnection(url,"sa","sa");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
        Connection con=null;
Statement ps1=null;
        Statement ps2=null;
       /* try {
ps1 = con.createStatement(ResultSet.
                         TYPE_SCROLL_INSENSITIVE,
                         ResultSet.CONCUR_READ_ONLY);
} catch (SQLException e1) {
e1.printStackTrace();
}*/
        try {
         ps1.executeUpdate("Insert Into T_ZB25 Values('" +
        getbarcode.getText().trim() +
        "','" + getpapername.getText().trim() + "','" +
        getpartbom.getText().trim() +
        "','" + getbarcode.getText().trim() + 
        "','"+getpici.getText().trim()+"','"+
        getinkutime.getText().trim()+
        "','");
} catch (SQLException e1) {
e1.printStackTrace();
}

}
}
if(ex.getSource()==delete){
if(getbarcode.getText().trim().equals(""))
JOptionPane.showMessageDialog(null,"请输入条形码!");
else if(DBruku.isfoundbook(getbarcode.getText().trim())==0)
JOptionPane.showMessageDialog(null,"该图纸不存在!");
else{
DBruku.delbook(getbarcode.getText().trim());
JOptionPane.showMessageDialog(null,"删除成功!");
}
}
if(ex.getSource()==reset){
getpapername.setText("");
getmachine_part_code.setText("");
getbarcode.setText("");
getpartcode.setText("");
getpartbom.setText("");
getpici.setText("");
//getpdate.setText("");
}
if(ex.getSource()==out){
Main frm=new Main();
frm.setVisible(true);
this.setVisible(false);
}

}

public static void main(String arg[]){
JFrame.setDefaultLookAndFeelDecorated(true);
ZB25IN t=new ZB25IN();
t.setVisible(true);
}

}
我想通过输入框输入,将数据写入数据库指定位置?以下是运行过程中错误源码:
[Microsoft][ODBC SQL Server Driver]语法错误或违反访问规则
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TechBook.ZB25IN.actionPerformed(ZB25IN.java:133)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
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(Unknown Source)

解决方案 »

  1.   

    你public void actionPerformed这个方法中被注释的乱七八糟的,这个是你真实的程序吗,如果是这样肯定不能运行啊,好几个地方都被你注释了,而且变量的作用于定义的也不对啊。
      

  2.   

    sql不对
            "','"+getpici.getText().trim()+"','"+
            getinkutime.getText().trim()+
            "','"); 
    --->如果你的字段类型都是字符串的话。如果有int,相应去掉单引号
            "','"+getpici.getText().trim()+"','"+
            getinkutime.getText().trim()+
            "')");