当前位置:编程学习 > JAVA >>

爪哇小题目

这是一道参照书上敲代码,可是有错误,求大神帮忙。我数据库表已经创好,连接测试页成功,可是运行后点击添加没有反应,代码没进入数据库,求大神帮忙~求速度~


import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class Addstudent extends JFrame implements ActionListener {
JLabel JL=new JLabel("添加基本信息",JLabel.CENTER);
JLabel JLNumber=new JLabel("学号");
JTextField JTNumber=new JTextField();
JLabel JLName=new JLabel("姓名");
JTextField JTName=new JTextField();
JLabel JLClass=new JLabel("班级");
JTextField JTClass=new JTextField();
JLabel JL1=new JLabel("学院");
JTextField JT1=new JTextField();
JLabel JLSex=new JLabel("性别");
ButtonGroup BG=new ButtonGroup();
JRadioButton JRB1=new JRadioButton("男");
JRadioButton JRB2=new JRadioButton("女");
JButton JBAdd=new JButton("添加");
JButton JBNext=new JButton("重置");
String sql="";
public Addstudent()
{
this.setTitle("添加学生信息");
this.setLayout(null);
JL.setBounds(100,30,200,40);
this.add(JL);
JL.setBounds(100,30,200,40);
this.add(JLNumber);
JLNumber.setBounds(100, 80, 100, 20);
this.add(JTNumber);
JTNumber.setBounds(200,80,80,20);
this.add(JLName);
JLName.setBounds(100,120,60,20);
this.add(JTName);
JTName.setBounds(200,120,80,20);
this.add(JLSex);
JLSex.setBounds(100,160,100,20);
JRB1.setBounds(200,160,40,20);
JRB2.setBounds(300,160,40,20);
this.add(JRB1);
this.add(JRB2);
BG.add(JRB1);
BG.add(JRB2);
JLClass.setBounds(100,240,60,20);
this.add(JLClass);
JTClass.setBounds(200,240,80,20);
this.add(JTClass);
JL1.setBounds(100,280,80,20);
this.add(JL1);
JT1.setBounds(200,280,80,20);
this.add(JT1);
JBAdd.setBounds(80,320,60,20);
this.add(JBAdd);
JBAdd.addActionListener(this);
JBNext.setBounds(190, 320, 90, 20);
this.add(JBNext);
JBNext.addActionListener(this);
this.setBounds(10,10,500,400);
this.setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==JBAdd)
{
String snumber=JTNumber.getText();
String sname=JTName.getText();
String sclass=JTClass.getText();
String ssex;
if(JRB1.isSelected())
{
ssex="男";
}
else
{
ssex="女";
}
String scollect=JT1.getText();

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cot=DriverManager.getConnection("jdbc:odbc:student1","","");
Statement stm=cot.createStatement();
ResultSet rs=stm.executeQuery(sql);
if(rs.next())
JOptionPane.showMessageDialog(null,"该帐号已存在!");
else
{

sql="insert student1 values('"+snumber+"','"+sname+"','"+sclass+"','"+ssex+"','"+scollect+"')";
int i=stm.executeUpdate(sql);
if(i>0)
{
JOptionPane.showMessageDialog(null, "添加成功");
}
else
{
JOptionPane.showMessageDialog(null, "删除失败");
}
}
}
catch(Exception ee)
{

}
}
if(e.getSource()==JBNext)
{
JTNumber.setText(null);
JTName.setText(null);
JTClass.setText(null);
JT1.setText(null);
}
}

public static void main(String[] args) {
new Addstudent();

}
} --------------------编程问答-------------------- 你的代码不全吧。。。

ResultSet rs=stm.executeQuery(sql);//这里面的sql呢。。。

还有catch(Exception ee)里加一句打印异常的,
说不定已经抛异常了呢。。

还有conn.commit()之类的检查下。。。
--------------------编程问答-------------------- catch(Exception ee)
{

}
里面请打印日志,有可能是异常了 --------------------编程问答-------------------- 怎么不打日志呢,同上 --------------------编程问答-------------------- 我感觉参照书基本都是会省略一些东西让你自己去加的……    --------------------编程问答-------------------- sql文初期化不对,ResultSet rs=stm.executeQuery(sql);执行时,sql是空串,你在这句代码加上下面一句应该就可以了。
sql="select * from student1"; --------------------编程问答-------------------- import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class Addstudent extends JFrame implements ActionListener {
JLabel JL = new JLabel("Add base info", JLabel.LEFT);
JLabel JLNumber = new JLabel("studentId");
JTextField JTNumber = new JTextField();
JLabel JLName = new JLabel("Name");
JTextField JTName = new JTextField();
JLabel JLClass = new JLabel("Class");
JTextField JTClass = new JTextField();
JLabel JL1 = new JLabel("School");
JTextField JT1 = new JTextField();
JLabel JLSex = new JLabel("Sex");
ButtonGroup BG = new ButtonGroup();
JRadioButton JRB1 = new JRadioButton("male");
JRadioButton JRB2 = new JRadioButton("female");
JButton JBSch = new JButton("SCH");
JButton JBAdd = new JButton("ADD");
JButton JBDel = new JButton("DEL");
JButton JBNext = new JButton("RESET");
String sql = "select * from student1";

public Addstudent() {
this.setTitle("Add Student Info");
this.setLayout(null);
this.add(JL);
JL.setBounds(20, 0, 200, 40);
this.add(JLNumber);
JLNumber.setBounds(20, 40, 60, 20);
this.add(JTNumber);
JTNumber.setBounds(80, 40, 80, 20);
this.add(JLName);
JLName.setBounds(20, 60, 60, 20);
this.add(JTName);
JTName.setBounds(80, 60, 80, 20);
this.add(JLSex);
JLSex.setBounds(20, 80, 60, 20);
JRB1.setBounds(80, 80, 60, 20);
JRB2.setBounds(140, 80, 80, 20);
this.add(JRB1);
this.add(JRB2);
BG.add(JRB1);
BG.add(JRB2);
JLClass.setBounds(20, 100, 60, 20);
this.add(JLClass);
JTClass.setBounds(80, 100, 80, 20);
this.add(JTClass);
JL1.setBounds(20, 120, 60, 20);
this.add(JL1);
JT1.setBounds(80, 120, 80, 20);
this.add(JT1);

JBSch.setBounds(20, 320, 60, 20);
this.add(JBSch);
JBSch.addActionListener(this);

JBAdd.setBounds(90, 320, 60, 20);
this.add(JBAdd);
JBAdd.addActionListener(this);

JBDel.setBounds(160, 320, 60, 20);
this.add(JBDel);
JBDel.addActionListener(this);

JBNext.setBounds(230, 320, 90, 20);
this.add(JBNext);
JBNext.addActionListener(this);

this.setBounds(10, 10, 500, 400);
this.setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public void actionPerformed(ActionEvent e) {
Connection cot = null;
Statement stm = null;
// reset process
if (e.getSource() == JBNext) {
JTNumber.setText(null);
JTNumber.setBackground(Color.white);
JTName.setText(null);
JTClass.setText(null);
JT1.setText(null);
}else{
String snumber = JTNumber.getText();
String sname = JTName.getText();
String sclass = JTClass.getText();
String ssex;
if (JRB1.isSelected()) {
ssex = "male";
} else {
ssex = "female";
}
String scollect = JT1.getText();
JTNumber.setBackground(Color.white);
//Select process
if (e.getSource() == JBSch) {
try {
Class.forName("org.gjt.mm.mysql.Driver");
cot = DriverManager.getConnection(
"jdbc:mysql://localhost/mydb", "root", "xibo");
stm = cot.createStatement();
//record get
sql = "select * from student1 where studentId='"+snumber+"'";
ResultSet rs = stm.executeQuery(sql);
if(!rs.next()){
JOptionPane.showMessageDialog(null, "The accent does not exist、Please input another!");
JTNumber.setBackground(Color.red);
} else {
JTName.setText(rs.getString(2));
JTClass.setText(rs.getString(3));
JT1.setText(rs.getString(5));
}
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(stm != null){
try{
stm.close();
}catch(SQLException se){
se.printStackTrace();
}
}

if(cot != null){
try{
cot.close();
}catch(SQLException se){
se.printStackTrace();
}
}
}
}

//Delete process
if (e.getSource() == JBDel) {
try {
Class.forName("org.gjt.mm.mysql.Driver");
cot = DriverManager.getConnection(
"jdbc:mysql://localhost/mydb", "root", "xibo");
stm = cot.createStatement();
//record get
sql = "delete from student1 where studentId='"+snumber+"'";
int cont = stm.executeUpdate(sql);
if(cont == 1){
JOptionPane.showMessageDialog(null, "Delete sucess!");
} else {
JOptionPane.showMessageDialog(null, "Delete fail!");
}
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(stm != null){
try{
stm.close();
}catch(SQLException se){
se.printStackTrace();
}
}

if(cot != null){
try{
cot.close();
}catch(SQLException se){
se.printStackTrace();
}
}
}
}

//Insert process
if (e.getSource() == JBAdd) {
try {
Class.forName("org.gjt.mm.mysql.Driver");
cot = DriverManager.getConnection(
"jdbc:mysql://localhost/mydb", "root", "xibo");
stm = cot.createStatement();
//exists check
sql = "select * from student1 where studentId='"+snumber+"'";
ResultSet rs = stm.executeQuery(sql);
if(rs.next()){
JOptionPane.showMessageDialog(null, "The accent exists、Please input another!");
JTNumber.setBackground(Color.red);
} else {
stm = cot.createStatement();
sql = "insert student1 values('" + snumber + "','"
+ sname + "','" + sclass + "','" + ssex + "','"
+ scollect + "')";
int i = stm.executeUpdate(sql);
if (i > 0) {
JOptionPane.showMessageDialog(null, "Add Sucess");
} else {
JOptionPane.showMessageDialog(null, "Delete fail");
}
}
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(stm != null){
try{
stm.close();
}catch(SQLException se){
se.printStackTrace();
}
}

if(cot != null){
try{
cot.close();
}catch(SQLException se){
se.printStackTrace();
}
}
}
}
}
}

/**
 * @param args
 */
public static void main(String[] args) {
new Addstudent();
}
}
--------------------编程问答-------------------- 我表示吧。。可能是一个非常2的地方,Connection ct=DriverManager.getConnection(“jdbc:odbc:testsp”,”scott”,”m123”);这后面的用户名和密码你不会真的没写吧?SQL语句那里没错。。他是后面用了个rs读取结果去判断的。 --------------------编程问答-------------------- 看到awt 就突然好怀念做swing的日子
补充:Java ,  Eclipse
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,