首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

求各位大神帮小弟指正代码中的异常!多谢

2013-10-24 
求各位大神帮小弟指正代码中的错误!!!谢谢package xiaomoimport java.awt.*import java.awt.BorderLayou

求各位大神帮小弟指正代码中的错误!!!谢谢

package xiaomo;

import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.*;

import javax.swing.*;

public class MessageFace extends JFrame{
public void CreateJFrame(String title){

//文本框的编辑
JTextField jt = new JTextField("Please enter static words to search...");
jt.setBackground(Color.pink);

//表格的设计
//JTable 的动态更新
String[] columnNames={"ID","Name","MobilePhone","Company","QQ number",};
String[][] tableValues={{"A1","B1","","",""},{"A2","B2","","",""},
{"A3","B3","","",""},{"name-x","B4","","",""},{"A5","xxxxxxxxxxx","","",""},
{"A3","B3","","",""},{"name-x","B4","","",""},{"A5","B5","","",""},
{"A3","B3","","",""},{"name-x","B4","","",""},{"A5","B5","","",""},
{"A3","B3","","",""},{"name-x","B4","","",""},{"A5","B5","","",""},
{"A3","B3","","",""},{"name-x","B4","","",""},{"A5","B5","","",""},
{"A3","B3","","",""},{"name-x","B4","","",""},{"A5","B5","","",""},
{"A3","B3","","",""},{"name-x","B4","","",""},{"A5","B5","","",""},
{"A3","B3","","",""},{"name-x","B4","","",""},{"A5","B5","","",""},
{"A3","B3","","",""},{"name-x","B4","","",""},{"A5","B5","","",""},
{"A3","B3","","",""},{"name-x","B4","","",""},{"A5","B5","","",""},
{"A3","B3","","",""},{"name-x","B4","","",""},{"A5","B5","","",""},
{"A3","B3","","",""},{"name-x","B4","","",""},{"A5","B5","","",""},
{"A3","B3","","",""},{"name-x","B4","","",""},{"A5","B5","","",""},
{"A3","B3","","",""},{"name-x","B4","","",""},{"A5","B5","","",""},
{"A3","B3","","",""},{"name-x","B4","","",""},{"A5","B5","","",""},
};
JTable table = new JTable(tableValues, columnNames);
JScrollPane scrollPane = new JScrollPane(table);

//对窗体的预处理
JFrame jf = new JFrame(title);
Container container = jf.getContentPane();
container.setBackground(Color.green);

//jf.setLayout(null);
jf.setBounds(900, 100, 415, 472);//(x,y,width,weight)
jf.setVisible(true);
//jf.setResizable(false);  //去掉最大化
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

//将面板划分为three个容器
JPanel jp1 = new JPanel(new GridLayout(1,1));
JPanel jp2 = new JPanel(new GridLayout(1,5,5,5));
JPanel jp3 = new JPanel(new GridLayout(1,1));
//组件所在副容器

//jp2的编辑
jp2.setBackground(Color.darkGray);
JButton jb1 = new JButton("Build");
jb1.setBackground(Color.lightGray);
JButton jb2 = new JButton("Edit");
JButton jb3 = new JButton("Delete");
JButton jb4 = new JButton("Search");
JButton jb5 = new JButton("Back");

jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new BuildButton(MessageFace.this,"").setVisible(true);
}
});

/*
 * 在设计代码时,要注重  程序的先后顺序,语言逻辑    否则就会出现启动程序时不显示,
 * 拖动一下才会显示的怪现象,其实这是逻辑的问题!!!
 */
jp1.add(scrollPane,BorderLayout.CENTER);
jp2.add(jb1);
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
jp2.add(jb5);
jp3.add(jt);
container.add(jp3,BorderLayout.NORTH);
container.add(jp1,BorderLayout.CENTER);
container.add(jp2,BorderLayout.SOUTH);

}
public static void main(String[] args){
// TODO Auto-generated method stub
new MessageFace().CreateJFrame("People");
}


}

/*-----------------------------------------------------------------------*/
package xiaomo;

import java.sql.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class BuildButton extends JDialog{
static Connection con;
static PreparedStatement sql;

public BuildButton(MessageFace messageface,String title){

super(messageface,"Please finish writing .",true);

//利用箱式布局管理器设计Name栏mb
//Birthday标签
Box IDBox = Box.createHorizontalBox();
IDBox.add(Box.createHorizontalStrut(30));
JLabel IDLabel = new JLabel("ID:");
IDBox.add(IDLabel);

IDBox.add(Box.createHorizontalStrut(20));
final JTextField IDTextField = new JTextField(30);
IDBox.add(IDTextField);
IDBox.add(Box.createHorizontalStrut(200));

//Name标签
Box NameBox = Box.createHorizontalBox();
//container.add(NameBox,BorderLayout.NORTH);
NameBox.add(Box.createHorizontalStrut(30));
JLabel NameLabel = new JLabel("NAME:");
NameBox.add(NameLabel);




NameBox.add(Box.createHorizontalStrut(20));
final JTextField NameTextField = new JTextField(30);
NameBox.add(NameTextField);
NameBox.add(Box.createHorizontalStrut(200));

//Number标签
Box NumberBox = Box.createHorizontalBox();
NumberBox.add(Box.createHorizontalStrut(30));
JLabel NumberLabel = new JLabel("MobilePhone:");
NumberBox.add(NumberLabel);

NumberBox.add(Box.createHorizontalStrut(20));
final JTextField NumberTextField = new JTextField(30);
NumberBox.add(NumberTextField);
NumberBox.add(Box.createHorizontalStrut(200));

//QQNumber标签
Box QQNumberBox = Box.createHorizontalBox();
QQNumberBox.add(Box.createHorizontalStrut(30));
JLabel QQNumberLabel = new JLabel("QQNumber:");
QQNumberBox.add(QQNumberLabel);

QQNumberBox.add(Box.createHorizontalStrut(20));
final JTextField QQNumberTextField = new JTextField(30);
QQNumberBox.add(QQNumberTextField);
QQNumberBox.add(Box.createHorizontalStrut(200));

//Company标签
Box CompanyBox = Box.createHorizontalBox();
CompanyBox.add(Box.createHorizontalStrut(30));
JLabel CompanyLabel = new JLabel("Company:");
CompanyBox.add(CompanyLabel);

CompanyBox.add(Box.createHorizontalStrut(20));
final JTextField CompanyTextField = new JTextField(30);
CompanyBox.add(CompanyTextField);
CompanyBox.add(Box.createHorizontalStrut(200));

JButton b1 = new JButton("Finish");
JButton b2 = new JButton("Continue");

/*
 * 要想使得JPanel面板划分窗体空间,
 * 就必须使用网格布局管理器GridLayout和边界布局管理器BorderLayout
 * 而且在添加组件是要注意:
 * 先在container里对面板进行整体设置
 * 而后在个面板中设置GridLayout
 * 最后再添加到container中...
 */


//对container进行设置
Container container = getContentPane();
container.setLayout(new GridLayout(6,2,8,9));
setBounds(900, 50, 415, 220);
container.setBackground(Color.pink);

//对面板(容器的一种)进行设置
JPanel p1 = new JPanel(new GridLayout(1,2,8,9));//行,列,水平,垂直
p1.setBackground(Color.pink);
JPanel p2 = new JPanel(new GridLayout(1,2,8,9));
p2.setBackground(Color.pink);
JPanel p3 = new JPanel(new GridLayout(1,2,8,9));
p3.setBackground(Color.pink);
JPanel p4 = new JPanel(new GridLayout(1,2,8,9));
p4.setBackground(Color.pink);
JPanel p5 = new JPanel(new GridLayout(1,2,8,9));
p5.setBackground(Color.pink);
JPanel p6 = new JPanel(new GridLayout(1,2));

//将组件添加到面板,再添加到container
p1.add(IDBox,BorderLayout.NORTH);
p1.add(IDTextField);

p2.add(NameBox,BorderLayout.NORTH);
p2.add(NameTextField);

p3.add(NumberBox,BorderLayout.NORTH);
p3.add(NumberTextField);

p4.add(QQNumberBox,BorderLayout.NORTH);
p4.add(QQNumberTextField);

p5.add(CompanyBox,BorderLayout.NORTH);
p5.add(CompanyTextField);

p6.add(b1,BorderLayout.NORTH);
p6.add(b2,BorderLayout.NORTH);

container.add(p1);
container.add(p2);
container.add(p3);
container.add(p4);
container.add(p5);
container.add(p6);

b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){



try {

sql = con.prepareStatement("insert into xiaomotable" + "values(?,?,?,?,?)");
sql.setString(1, IDTextField.getText());
sql.setString(2, NameTextField.getText());
sql.setString(3, NumberTextField.getText());
sql.setString(4, QQNumberTextField.getText());
sql.setString(5, CompanyTextField.getText());

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

}

});
}
}


/*---------------------------------------------------------------------*/
package xiaomo;
import java.sql.Connection;  
import java.sql.DriverManager;  
class connect{  
static Connection con;
   public static void main(String args[]){
   
    String driver = "com.mysql.jdbc.Driver";//驱动程序名  
    String url = "jdbc:mysql://127.0.0.1:3306/xiaomo";  
    String user = "root"; 
    String password = "";  
    try {         // 加载驱动程序  
    Class.forName(driver);  
    Connection con = DriverManager.getConnection(url, user, password);// 连续数据库  
    System.out.println("数据库连接成功!");
    }catch(Exception e){  
        System.out.println("数据库连接失败!");  


        e.printStackTrace();          
    }  
    
  }  



哪位高人可以帮小弟处理上面的代码,其中第二个代码在运行时有错,我本想实现对build按钮功能,它的功能是:提交填入的数据到mysql中。但是一按build按钮就报错,下面是错误信息(望各位大神详细指点或是劳心帮小弟修改一下,小弟非常感谢!!!) java mysql Eclipse
[解决办法]
sql = con.prepareStatement("insert into xiaomotable" + "values(?,?,?,?,?)");
有两个错误
1、con没有赋值为null,所以报空指针异常
2、"insert into xiaomotable" + "values(?,?,?,?,?)" 
应该写成"insert into xiaomotable" + " values(?,?,?,?,?)"  xiaomotable和values之间应该有一个空格

热点排行