高分求助JAVA高手
我再做一个学生学籍管理的JAVA程序要求实现操作连接数据库实现查询,增,删,改现在已经把图画出来咯但是报错说主函数异常传代码上来,希望高手指导一下然后就是给我说一下要怎样...
我再做一个学生学籍管理的JAVA程序
要求实现操作连接数据库实现查询,增,删,改
现在已经把图画出来咯
但是报错说主函数异常
传代码上来,希望高手指导一下
然后就是给我说一下要怎样才能实现将数据输出在表格中
谢谢
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Win extends JFrame {
JTable table;
Object a[][];
Object name[] = {"学期","科目","成绩"};
JButton sele,upde,inse,dele;
JTextField inputNumber1,inputNumber2;
int rows = 1;
JPanel p;
Win (){
sele = new JButton("查询");
upde = new JButton("添加");
a = new Object[rows][3];
table = new JTable(a,name);
p = new JPanel();
p.add(new JLabel("学号"));
p.add(inputNumber1);
p.add(sele);
p.add(upde);
add(p,BorderLayout.NORTH);
inse = new JButton("修改");
dele = new JButton("删除");
p = new JPanel();
p.add(new JLabel("姓名"));
p.add(inputNumber2);
p.add(inse);
p.add(dele);
add(p,BorderLayout.CENTER);
add(new JScrollPane(table),BorderLayout.SOUTH);
setSize(550,200);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Student{
public static void main(String args[])
{
Win win = new Win();
}
}
1楼的高手~
照你的改咯~
但是只出现了一个文本框和两个按钮~
我画的是出现两个文本框和四个按钮~还有一个表格~
望解答~谢谢~
我刚刚把大小改了下就出来咯~
现在的问题就是要通过哪种方法使SQL的查询结果到表格中哦~
谢谢~ 展开
要求实现操作连接数据库实现查询,增,删,改
现在已经把图画出来咯
但是报错说主函数异常
传代码上来,希望高手指导一下
然后就是给我说一下要怎样才能实现将数据输出在表格中
谢谢
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Win extends JFrame {
JTable table;
Object a[][];
Object name[] = {"学期","科目","成绩"};
JButton sele,upde,inse,dele;
JTextField inputNumber1,inputNumber2;
int rows = 1;
JPanel p;
Win (){
sele = new JButton("查询");
upde = new JButton("添加");
a = new Object[rows][3];
table = new JTable(a,name);
p = new JPanel();
p.add(new JLabel("学号"));
p.add(inputNumber1);
p.add(sele);
p.add(upde);
add(p,BorderLayout.NORTH);
inse = new JButton("修改");
dele = new JButton("删除");
p = new JPanel();
p.add(new JLabel("姓名"));
p.add(inputNumber2);
p.add(inse);
p.add(dele);
add(p,BorderLayout.CENTER);
add(new JScrollPane(table),BorderLayout.SOUTH);
setSize(550,200);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Student{
public static void main(String args[])
{
Win win = new Win();
}
}
1楼的高手~
照你的改咯~
但是只出现了一个文本框和两个按钮~
我画的是出现两个文本框和四个按钮~还有一个表格~
望解答~谢谢~
我刚刚把大小改了下就出来咯~
现在的问题就是要通过哪种方法使SQL的查询结果到表格中哦~
谢谢~ 展开
6个回答
展开全部
典型的空指针错误inputNumber1,inputNumber2没有初始化。改为:
JTextField inputNumber1 =new JTextField("",10), inputNumber2=new JTextField("",10);
程序如下(已经为你调试通了,还能出现界面):
--------------------------------------------
....(省略)
--------------------------------------------
补充回答1:你把窗口往大设置点如下:setSize(700, 800);
----------------------
....(省略)
---------------------------------------------
补充回答2:搂主你好贪心阿,一连串的问题问个没完:给jtable设置值可以通过setValueAt
(object,int,int);
如本问题可设置可这样设置置:
在语句table = new JTable(a, name);后面补充:
table.setValueAt("98",0,0);
table.setValueAt("82",0,1);
table.setValueAt("72",0,2);
就可把成绩98,82,72加入,具体从数据库查询出来的值也可以类似加入!!
-------------------------------------------
补充回答3:看来我不回答你你还真不给分,现在我就回答你:我给你用Access连接好了,你在该java目
录下建立Student.mdb数据库,然后建立一张StuMarkMng表(注意这些名字必须和我说的一致)如下:
StuMarkMng (表名)
属性:StuID(学号),StuName(姓名),StuMark(成绩)
填入记录:040450107,冯某,98。
然后直接运行程序,点击查询就可以了。(本程序连接数据库不需要建立数据源)
如果你还是不会的话告诉我你的邮箱地址,我给你直接发过去。我只给你做了个查询,其他你可以类似的
做下去。程序代码给你修改如下:
---------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class Win extends JFrame
{
JTable table;
Object a[][];
Object name[] = { "学号", "姓名", "成绩" };
JButton sele, upde, inse, dele;
JTextField inputNumber1 =new JTextField("040450107",10), inputNumber2=new JTextField("",10);
int rows = 1;
JPanel p;
LinkAccess linkaccess = new LinkAccess();
ResultSet rs=null;
Win()
{
sele = new JButton("查询");
sele.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent avt)
{
try
{
rs=linkaccess.Query("select * from StuMarkMng where
StuID='"+inputNumber1.getText().trim()+"'");
if(rs!=null)
{
rs.next();
table.setValueAt(rs.getString("StuID"),0,0);
table.setValueAt(rs.getString("StuName"),0,1);
table.setValueAt(rs.getInt("StuMark"),0,2);
}
}
catch(Exception e){e.printStackTrace();}
}
}
);
upde = new JButton("添加");
a = new Object[rows][3];
table = new JTable(a, name);
p = new JPanel();
p.add(new JLabel("学号"));
p.add(inputNumber1);
p.add(sele);
p.add(upde);
add(p, BorderLayout.NORTH);
inse = new JButton("修改");
dele = new JButton("删除");
p = new JPanel();
p.add(new JLabel("姓名"));
p.add(inputNumber2);
p.add(inse);
p.add(dele);
add(p, BorderLayout.CENTER);
add(new JScrollPane(table), BorderLayout.SOUTH);
setSize(700, 800);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Student
{
public static void main(String args[])
{
Win win = new Win();
}
}
//数据库连接
//----------------------------------------------------------------------------------
class LinkAccess
{
String conStr;
Connection con;
ResultSet rs;
Statement stm;
public LinkAccess()
{
conStr="jdbc:odbc:driver={MicroSoft Access Driver (*.mdb)};DBQ=Student.mdb";
//String sConnStr = "jdbc:odbc:driver={MicroSoft Access Driver
(*.mdb)};DBQ=test.mdb";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(conStr,"sa","sa");
stm=con.createStatement();
}
catch(SQLException e){e.printStackTrace();}
catch(ClassNotFoundException e){e.printStackTrace();}
}
//查询数据库方法
public ResultSet Query(String sql)
{
String sb = new String("");
try
{
rs=stm.executeQuery(sql);
}
catch(Exception e){e.printStackTrace();}
System.out.println("出据库查询完毕!");
return rs;
}
//更改数据库操作
public void updateData(String sql)
{
System.out.println(sql);
try{stm.executeUpdate(sql);}catch(Exception e){e.printStackTrace();}
}
}
//----------------------------------
JTextField inputNumber1 =new JTextField("",10), inputNumber2=new JTextField("",10);
程序如下(已经为你调试通了,还能出现界面):
--------------------------------------------
....(省略)
--------------------------------------------
补充回答1:你把窗口往大设置点如下:setSize(700, 800);
----------------------
....(省略)
---------------------------------------------
补充回答2:搂主你好贪心阿,一连串的问题问个没完:给jtable设置值可以通过setValueAt
(object,int,int);
如本问题可设置可这样设置置:
在语句table = new JTable(a, name);后面补充:
table.setValueAt("98",0,0);
table.setValueAt("82",0,1);
table.setValueAt("72",0,2);
就可把成绩98,82,72加入,具体从数据库查询出来的值也可以类似加入!!
-------------------------------------------
补充回答3:看来我不回答你你还真不给分,现在我就回答你:我给你用Access连接好了,你在该java目
录下建立Student.mdb数据库,然后建立一张StuMarkMng表(注意这些名字必须和我说的一致)如下:
StuMarkMng (表名)
属性:StuID(学号),StuName(姓名),StuMark(成绩)
填入记录:040450107,冯某,98。
然后直接运行程序,点击查询就可以了。(本程序连接数据库不需要建立数据源)
如果你还是不会的话告诉我你的邮箱地址,我给你直接发过去。我只给你做了个查询,其他你可以类似的
做下去。程序代码给你修改如下:
---------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class Win extends JFrame
{
JTable table;
Object a[][];
Object name[] = { "学号", "姓名", "成绩" };
JButton sele, upde, inse, dele;
JTextField inputNumber1 =new JTextField("040450107",10), inputNumber2=new JTextField("",10);
int rows = 1;
JPanel p;
LinkAccess linkaccess = new LinkAccess();
ResultSet rs=null;
Win()
{
sele = new JButton("查询");
sele.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent avt)
{
try
{
rs=linkaccess.Query("select * from StuMarkMng where
StuID='"+inputNumber1.getText().trim()+"'");
if(rs!=null)
{
rs.next();
table.setValueAt(rs.getString("StuID"),0,0);
table.setValueAt(rs.getString("StuName"),0,1);
table.setValueAt(rs.getInt("StuMark"),0,2);
}
}
catch(Exception e){e.printStackTrace();}
}
}
);
upde = new JButton("添加");
a = new Object[rows][3];
table = new JTable(a, name);
p = new JPanel();
p.add(new JLabel("学号"));
p.add(inputNumber1);
p.add(sele);
p.add(upde);
add(p, BorderLayout.NORTH);
inse = new JButton("修改");
dele = new JButton("删除");
p = new JPanel();
p.add(new JLabel("姓名"));
p.add(inputNumber2);
p.add(inse);
p.add(dele);
add(p, BorderLayout.CENTER);
add(new JScrollPane(table), BorderLayout.SOUTH);
setSize(700, 800);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Student
{
public static void main(String args[])
{
Win win = new Win();
}
}
//数据库连接
//----------------------------------------------------------------------------------
class LinkAccess
{
String conStr;
Connection con;
ResultSet rs;
Statement stm;
public LinkAccess()
{
conStr="jdbc:odbc:driver={MicroSoft Access Driver (*.mdb)};DBQ=Student.mdb";
//String sConnStr = "jdbc:odbc:driver={MicroSoft Access Driver
(*.mdb)};DBQ=test.mdb";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(conStr,"sa","sa");
stm=con.createStatement();
}
catch(SQLException e){e.printStackTrace();}
catch(ClassNotFoundException e){e.printStackTrace();}
}
//查询数据库方法
public ResultSet Query(String sql)
{
String sb = new String("");
try
{
rs=stm.executeQuery(sql);
}
catch(Exception e){e.printStackTrace();}
System.out.println("出据库查询完毕!");
return rs;
}
//更改数据库操作
public void updateData(String sql)
{
System.out.println(sql);
try{stm.executeUpdate(sql);}catch(Exception e){e.printStackTrace();}
}
}
//----------------------------------
展开全部
用眼睛还是看不出来
水平不够
水平不够
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
好久不做swing的了
学习一下。
学习一下。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
@SuppressWarnings("serial")
class MainFrm extends JFrame
{
private JButton DisplayButton, InsertButton, UpdateButton, DeleteButton, nextButton, lastButton, freeQueryButton, exitbutton;
private JList EventNumberList;
private JTextField IDText, titleText, dateText, freeQueryText;
private Connection conn;
private Statement stmt;
private ResultSet rs;
private JLabel label;
private int CoursorPos;
public static void main(String[] args)
{
MainFrm event = new MainFrm();
event.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
event.Init();
event.buildGUI();
}
MainFrm()
{
super("DataBase Operation");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
}
catch(Exception e)
{
System.out.println("CatchException: [In MainFrm()] >> " + e);
System.exit(1);
}
}
void Init()
{
try
{
ConnectToDB TempConn = new ConnectToDB();
conn = TempConn.GetSQLConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
}
catch(Exception e)
{
System.out.println("CatchException: [In Init()] >> " + e);
}
}
//loadAccouts
private void loadAccounts()
{
Vector<String> v = new Vector<String>();
try
{
rs = stmt.executeQuery("SELECT * FROM Events");
while (rs.next())
{
v.addElement(rs.getString("id"));
}
}
catch (SQLException e)
{
System.out.println("CatchException: [In loadAccounts] >>" + e);
}
EventNumberList.setListData(v);
}
//buildGUI
@SuppressWarnings("deprecation")
private void buildGUI()
{
Container c = getContentPane();
c.setLayout(new FlowLayout());
EventNumberList = new JList();
loadAccounts();
EventNumberList.setVisibleRowCount(5);
EventNumberList.setFixedCellWidth(30);
JScrollPane accountNumberListScrollPane = new JScrollPane(EventNumberList);
freeQueryText = new JTextField(40);
DisplayButton = new JButton("Display");
DisplayButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try
{
if(rs.first())
{
while(rs.next())
{
if(rs.getString("id").equals(EventNumberList.getSelectedValue()))
break;
}
}
if(!rs.isAfterLast())
{
IDText.setText(rs.getString("id"));
titleText.setText(rs.getString("title"));
dateText.setText(rs.getString("date"));
}
}
catch(SQLException ex)
{
System.out.println("CatchException: [DisplayButton actionPerformed()] >> " + ex);
}
}
});
InsertButton = new JButton("Insert");
InsertButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Statement statement = conn.createStatement();
statement.executeUpdate("INSERT INTO Events VALUES('" + IDText.getText() + "','" + titleText.getText() + "','" + dateText.getText() + "')");
EventNumberList.removeAll();
loadAccounts();
}
catch (SQLException ex)
{
System.out.println("CatchException: [InsertButton actionPerformed()] >> " + ex);
}
}
});
DeleteButton = new JButton("Delete");
DeleteButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Statement statement = conn.createStatement();
statement.executeUpdate("DELETE FROM Events WHERE id = " + EventNumberList.getSelectedValue());
EventNumberList.removeAll();
loadAccounts();
}
catch(SQLException ex)
{
System.out.println("CatchException: [DeleteButton actionPerformed()] >> " + ex);
}
}
});
UpdateButton = new JButton("Update");
UpdateButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Statement statement = conn.createStatement();
statement.executeUpdate("UPDATE Events "
+ "SET id='" + IDText.getText() + "', "
+ "title='" + titleText.getText() + "', "
+ "date = '" + dateText.getText() + "' WHERE id = "
+ EventNumberList.getSelectedValue());
EventNumberList.removeAll();
loadAccounts();
CoursorPos = rs.getRow();
System.out.println(CoursorPos);
}
catch (SQLException ex)
{
System.out.println("CatchException: [UpdateButton actionPerformed()] >> " + ex);
}
}
});
nextButton = new JButton("Next");
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
try
{
if (!rs.isLast())
{
rs.next();
IDText.setText(rs.getString("id"));
titleText.setText(rs.getString("title"));
dateText.setText(rs.getString("date"));
}
else
{
System.out.println("Got Last!");
label.setText("get the lastest...");
}
}
catch (SQLException ex)
{
System.out.println("CatchException: [NextButton actionPerformed()] >> " + ex);
}
}
});
lastButton = new JButton("previous");
lastButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
try
{
if (!rs.isFirst())
{
rs.previous();
IDText.setText(rs.getString("id"));
titleText.setText(rs.getString("title"));
dateText.setText(rs.getString("date"));
}
else
{
System.out.println("Got First!");
label.setText("Get the firstest...");
}
}
catch (SQLException ex)
{
System.out.println("CatchException: [LastButton actionPerformed()] >> " + ex);
}
}
});
freeQueryButton = new JButton("Execute Query");
freeQueryButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Statement statement = null;
try
{
if (freeQueryText.getText().toUpperCase().indexOf("SELECT") >= 0)
{
statement = conn.createStatement();
rs = statement.executeQuery(freeQueryText.getText());
if (rs.next())
{
IDText.setText(rs.getString("id"));
titleText.setText(rs.getString("title"));
dateText.setText(rs.getString("date"));
}
}
else
{
statement = conn.createStatement();
statement.executeUpdate(freeQueryText.getText());
loadAccounts();
}
}
catch (SQLException ex)
{
System.out.println("CatchException: [FreeQueryButton actionPerformed()] >> " + ex);
}
}
});
exitbutton = new JButton("Exit");
exitbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
label.setText("Exiting...Wait...");
System.out.println("OVER");
System.exit(1);
}
});
label = new JLabel();
JPanel first = new JPanel();
first.add(accountNumberListScrollPane);
JPanel bu = new JPanel();
bu.add(DisplayButton);
bu.add(InsertButton);
bu.add(DeleteButton);
bu.add(UpdateButton);
IDText = new JTextField(15);
titleText = new JTextField(15);
dateText = new JTextField(15);
JPanel second = new JPanel();
second.setLayout(new GridLayout(6, 1));
second.add(IDText);
second.add(titleText);
second.add(dateText);
JPanel fourth = new JPanel();
fourth.add(lastButton);
fourth.add(nextButton);
JPanel fifth = new JPanel();
fifth.add(freeQueryText);
JPanel sixth = new JPanel();
sixth.add(label);
c.add(first);
c.add(bu);
c.add(second);
c.add(fourth);
c.add(fifth);
c.add(freeQueryButton);
c.add(exitbutton);
c.add(sixth);
setSize(500, 500);
show();
}
}
下面是连接文件
import java.sql.*;
class ConnectToDB
{
private Connection conn = null;
public Connection GetSQLConnection() throws ClassNotFoundException
{
try
{
Class c = Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:sqlserver1");
System.out.println("\nGotConnection: " + c.getName());
}
catch(SQLException e)
{
System.out.println("\nCatched Exception in GetSQLConnection()>>>>" + e);
}
return conn;
}
}
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
@SuppressWarnings("serial")
class MainFrm extends JFrame
{
private JButton DisplayButton, InsertButton, UpdateButton, DeleteButton, nextButton, lastButton, freeQueryButton, exitbutton;
private JList EventNumberList;
private JTextField IDText, titleText, dateText, freeQueryText;
private Connection conn;
private Statement stmt;
private ResultSet rs;
private JLabel label;
private int CoursorPos;
public static void main(String[] args)
{
MainFrm event = new MainFrm();
event.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
event.Init();
event.buildGUI();
}
MainFrm()
{
super("DataBase Operation");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
}
catch(Exception e)
{
System.out.println("CatchException: [In MainFrm()] >> " + e);
System.exit(1);
}
}
void Init()
{
try
{
ConnectToDB TempConn = new ConnectToDB();
conn = TempConn.GetSQLConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
}
catch(Exception e)
{
System.out.println("CatchException: [In Init()] >> " + e);
}
}
//loadAccouts
private void loadAccounts()
{
Vector<String> v = new Vector<String>();
try
{
rs = stmt.executeQuery("SELECT * FROM Events");
while (rs.next())
{
v.addElement(rs.getString("id"));
}
}
catch (SQLException e)
{
System.out.println("CatchException: [In loadAccounts] >>" + e);
}
EventNumberList.setListData(v);
}
//buildGUI
@SuppressWarnings("deprecation")
private void buildGUI()
{
Container c = getContentPane();
c.setLayout(new FlowLayout());
EventNumberList = new JList();
loadAccounts();
EventNumberList.setVisibleRowCount(5);
EventNumberList.setFixedCellWidth(30);
JScrollPane accountNumberListScrollPane = new JScrollPane(EventNumberList);
freeQueryText = new JTextField(40);
DisplayButton = new JButton("Display");
DisplayButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try
{
if(rs.first())
{
while(rs.next())
{
if(rs.getString("id").equals(EventNumberList.getSelectedValue()))
break;
}
}
if(!rs.isAfterLast())
{
IDText.setText(rs.getString("id"));
titleText.setText(rs.getString("title"));
dateText.setText(rs.getString("date"));
}
}
catch(SQLException ex)
{
System.out.println("CatchException: [DisplayButton actionPerformed()] >> " + ex);
}
}
});
InsertButton = new JButton("Insert");
InsertButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Statement statement = conn.createStatement();
statement.executeUpdate("INSERT INTO Events VALUES('" + IDText.getText() + "','" + titleText.getText() + "','" + dateText.getText() + "')");
EventNumberList.removeAll();
loadAccounts();
}
catch (SQLException ex)
{
System.out.println("CatchException: [InsertButton actionPerformed()] >> " + ex);
}
}
});
DeleteButton = new JButton("Delete");
DeleteButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Statement statement = conn.createStatement();
statement.executeUpdate("DELETE FROM Events WHERE id = " + EventNumberList.getSelectedValue());
EventNumberList.removeAll();
loadAccounts();
}
catch(SQLException ex)
{
System.out.println("CatchException: [DeleteButton actionPerformed()] >> " + ex);
}
}
});
UpdateButton = new JButton("Update");
UpdateButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Statement statement = conn.createStatement();
statement.executeUpdate("UPDATE Events "
+ "SET id='" + IDText.getText() + "', "
+ "title='" + titleText.getText() + "', "
+ "date = '" + dateText.getText() + "' WHERE id = "
+ EventNumberList.getSelectedValue());
EventNumberList.removeAll();
loadAccounts();
CoursorPos = rs.getRow();
System.out.println(CoursorPos);
}
catch (SQLException ex)
{
System.out.println("CatchException: [UpdateButton actionPerformed()] >> " + ex);
}
}
});
nextButton = new JButton("Next");
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
try
{
if (!rs.isLast())
{
rs.next();
IDText.setText(rs.getString("id"));
titleText.setText(rs.getString("title"));
dateText.setText(rs.getString("date"));
}
else
{
System.out.println("Got Last!");
label.setText("get the lastest...");
}
}
catch (SQLException ex)
{
System.out.println("CatchException: [NextButton actionPerformed()] >> " + ex);
}
}
});
lastButton = new JButton("previous");
lastButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
try
{
if (!rs.isFirst())
{
rs.previous();
IDText.setText(rs.getString("id"));
titleText.setText(rs.getString("title"));
dateText.setText(rs.getString("date"));
}
else
{
System.out.println("Got First!");
label.setText("Get the firstest...");
}
}
catch (SQLException ex)
{
System.out.println("CatchException: [LastButton actionPerformed()] >> " + ex);
}
}
});
freeQueryButton = new JButton("Execute Query");
freeQueryButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Statement statement = null;
try
{
if (freeQueryText.getText().toUpperCase().indexOf("SELECT") >= 0)
{
statement = conn.createStatement();
rs = statement.executeQuery(freeQueryText.getText());
if (rs.next())
{
IDText.setText(rs.getString("id"));
titleText.setText(rs.getString("title"));
dateText.setText(rs.getString("date"));
}
}
else
{
statement = conn.createStatement();
statement.executeUpdate(freeQueryText.getText());
loadAccounts();
}
}
catch (SQLException ex)
{
System.out.println("CatchException: [FreeQueryButton actionPerformed()] >> " + ex);
}
}
});
exitbutton = new JButton("Exit");
exitbutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
label.setText("Exiting...Wait...");
System.out.println("OVER");
System.exit(1);
}
});
label = new JLabel();
JPanel first = new JPanel();
first.add(accountNumberListScrollPane);
JPanel bu = new JPanel();
bu.add(DisplayButton);
bu.add(InsertButton);
bu.add(DeleteButton);
bu.add(UpdateButton);
IDText = new JTextField(15);
titleText = new JTextField(15);
dateText = new JTextField(15);
JPanel second = new JPanel();
second.setLayout(new GridLayout(6, 1));
second.add(IDText);
second.add(titleText);
second.add(dateText);
JPanel fourth = new JPanel();
fourth.add(lastButton);
fourth.add(nextButton);
JPanel fifth = new JPanel();
fifth.add(freeQueryText);
JPanel sixth = new JPanel();
sixth.add(label);
c.add(first);
c.add(bu);
c.add(second);
c.add(fourth);
c.add(fifth);
c.add(freeQueryButton);
c.add(exitbutton);
c.add(sixth);
setSize(500, 500);
show();
}
}
下面是连接文件
import java.sql.*;
class ConnectToDB
{
private Connection conn = null;
public Connection GetSQLConnection() throws ClassNotFoundException
{
try
{
Class c = Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:sqlserver1");
System.out.println("\nGotConnection: " + c.getName());
}
catch(SQLException e)
{
System.out.println("\nCatched Exception in GetSQLConnection()>>>>" + e);
}
return conn;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//哈哈,我帮你解决了拉,帮我++分!!
//Student.java
//Student.java
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class Student {
private String sno;
private String sname;
private String cxueqi;
private String cname;
private String cgrade;
public int i ;
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getCxueqi() {
return cxueqi;
}
public void setCxueqi(String cxueqi) {
this.cxueqi = cxueqi;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCgrade() {
return cgrade;
}
public void setCgrade(String cgrade) {
this.cgrade = cgrade;
}
public void insert(Student student) throws Exception {
String sql = "INSERT INTO student VALUES(?,?)" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,student.getSno()) ;
pstmt.setString(2,student.getSname()) ;
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch (Exception e)
{
// System.out.println(e) ;
throw new Exception("操作中出现错误!!!") ;
}
finally
{
dbc.close() ;
}
}
// 修改操作
public void update(Student student) throws Exception
{
String sql = "UPDATE student SET sname=? WHERE sno=?" ; //根据学号来更新名字
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(2,student.getSno()) ;
pstmt.setString(1,student.getSname()) ;
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作中出现错误!!!") ;
}
finally
{
dbc.close() ;
}
}
public void delete(String sno) throws Exception //根据sno来删除数据
{
String sql = "DELETE FROM student WHERE sno=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,sno) ;
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作中出现错误!!!") ;
}
finally
{
dbc.close() ;
}
}
public List queryBySno(String sno)
{
Student student = null;
//String sql = "SELECT student.sname,sc.cxueqi ,sc.cname, sc.grade FROM sc left OUTER JOIN student ON sc.sno = student.sno WHERE student.sno=?";
String sql = "SELECT student.sname,sc.cxueqi ,sc.cname, sc.grade FROM sc, student WHERE student.sno=? AND sc.sno = student.sno" ;
PreparedStatement pstmt = null;
DataBaseConnection dbc = null;
dbc = new DataBaseConnection();
List list = new ArrayList();
try
{
pstmt = dbc.getConnection().prepareStatement(sql);
pstmt.setString(1, sno);
ResultSet rs = pstmt.executeQuery();
i = 0;
while (rs.next())
{
student = new Student();
student.setSno(sno);
student.setSname(rs.getString(1));
student.setCxueqi(rs.getString(2));
student.setCname(rs.getString(3));
student.setCgrade(rs.getString(4));
i++;
list.add(student);
}
rs.close();
pstmt.close();
dbc.close();
} catch (Exception e)
{
e.printStackTrace();
}
return list;
}
}
//DataBaseConnection.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class DataBaseConnection
{
String DBDRIVER = "sun.jdbc.odbc.JdbcOdbcDriver" ;
String DBURL = "jdbc:odbc:student";
Connection conn = null ;
PreparedStatement pstmt = null ;
String sql = null ;
public DataBaseConnection() {
try{
Class.forName(DBDRIVER) ;
} catch(Exception e1) {
System.out.println("数据库驱动程序加载失败!!!") ;
}
try {
conn = DriverManager.getConnection(DBURL) ;
} catch(Exception e2)
{
System.out.println("数据库连接失败!!!") ;
}
}
public Connection getConnection() {
return this.conn ;
}
public void close()
{
try{
conn.close() ;
} catch(Exception e4)
{
System.out.println("数据库关闭失败!!!") ;
}
}
}
//TestStudent.java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class TestStudent extends JFrame implements ActionListener {
JTable table,table2;
Object a[][];
Object name[] = { "学号","姓名","学期","课程","成绩" };
JButton query, insert, update, delete;
JTextField sno, sname;
int rows = 1;
JPanel p;
TestStudent(String s) {
super(s);
query = new JButton("查询"); //查询用 query
insert = new JButton("添加"); //添加用insert
query.addActionListener(this);
insert.addActionListener(this);
sno = new JTextField(10);
a = new Object[15][5];
table = new JTable(a, name);
p = new JPanel();
p.add(new JLabel("学号"));
p.add(sno);
p.add(query);
p.add(insert);
getContentPane().add(p, BorderLayout.NORTH);
add(p, BorderLayout.NORTH);
update = new JButton("修改"); //修改用updata
delete = new JButton("删除"); //删除用delete
update.addActionListener(this);
delete.addActionListener(this);
sname = new JTextField(10);
p = new JPanel();
p.add(new JLabel("姓名"));
p.add(sname);
p.add(update);
p.add(delete);
getContentPane().add(p, BorderLayout.CENTER);
add(p, BorderLayout.CENTER);
/*
a = new Object[rows][3];
table2 = new JTable(a, name2);
getContentPane()
.add(new JScrollPane(table2), BorderLayout.SOUTH);
add(new JScrollPane(table2), BorderLayout.SOUTH);
setSize(425,800);
*/
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane()
.add(new JScrollPane(table), BorderLayout.SOUTH);
add(new JScrollPane(table), BorderLayout.SOUTH);
setSize(800,1000);
}
public void actionPerformed(ActionEvent e) {
Student student=new Student();
student.setSno(sno.getText());
int i = 0 ;
student.setSname(sname.getText());
//System.out.println(student.getSname());
if(e.getSource()==insert) {
try {
student.insert(student);
JOptionPane.showMessageDialog(null, "数据添加成功");
}
catch (Exception e1) {
e1.printStackTrace();
}
}
else if(e.getSource()==update)
{
try
{
student.update(student);
JOptionPane.showMessageDialog(null, "数据更新成功");
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
else if(e.getSource()==delete)
{
try
{
student.delete(student.getSno());
JOptionPane.showMessageDialog(null, "数据删除成功");
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
else if(e.getSource()==query)
{
try
{
List list=student.queryBySno(student.getSno());
for(int i1=0;i1<list.size();i1++)
{
Student temp=(Student) list.get(i1);
table.setValueAt(temp.getSno(), i1, 0);
table.setValueAt(temp.getSname(), i1, 1);
table.setValueAt(temp.getCxueqi(), i1, 2);
table.setValueAt(temp.getCname(), i1, 3);
table.setValueAt(temp.getCgrade(),i1, 4);
System.out.println(temp.getSno());
System.out.println(temp.getSname());
System.out.println(temp.getCxueqi());
System.out.println(temp.getCname());
System.out.println(temp.getCgrade());
}
JOptionPane.showMessageDialog(null, "数据查询成功");
//Student temp2=new Student();
//temp2=student.queryBySno(student.getSno());
//JOptionPane.showMessageDialog(null, "数据查询成功");
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
}
public static void main(String args[])
{
TestStudent ts = new TestStudent("学籍管理");
ts.pack();
}
}
//Student.java
//Student.java
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class Student {
private String sno;
private String sname;
private String cxueqi;
private String cname;
private String cgrade;
public int i ;
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getCxueqi() {
return cxueqi;
}
public void setCxueqi(String cxueqi) {
this.cxueqi = cxueqi;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCgrade() {
return cgrade;
}
public void setCgrade(String cgrade) {
this.cgrade = cgrade;
}
public void insert(Student student) throws Exception {
String sql = "INSERT INTO student VALUES(?,?)" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,student.getSno()) ;
pstmt.setString(2,student.getSname()) ;
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch (Exception e)
{
// System.out.println(e) ;
throw new Exception("操作中出现错误!!!") ;
}
finally
{
dbc.close() ;
}
}
// 修改操作
public void update(Student student) throws Exception
{
String sql = "UPDATE student SET sname=? WHERE sno=?" ; //根据学号来更新名字
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(2,student.getSno()) ;
pstmt.setString(1,student.getSname()) ;
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作中出现错误!!!") ;
}
finally
{
dbc.close() ;
}
}
public void delete(String sno) throws Exception //根据sno来删除数据
{
String sql = "DELETE FROM student WHERE sno=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,sno) ;
pstmt.executeUpdate() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("操作中出现错误!!!") ;
}
finally
{
dbc.close() ;
}
}
public List queryBySno(String sno)
{
Student student = null;
//String sql = "SELECT student.sname,sc.cxueqi ,sc.cname, sc.grade FROM sc left OUTER JOIN student ON sc.sno = student.sno WHERE student.sno=?";
String sql = "SELECT student.sname,sc.cxueqi ,sc.cname, sc.grade FROM sc, student WHERE student.sno=? AND sc.sno = student.sno" ;
PreparedStatement pstmt = null;
DataBaseConnection dbc = null;
dbc = new DataBaseConnection();
List list = new ArrayList();
try
{
pstmt = dbc.getConnection().prepareStatement(sql);
pstmt.setString(1, sno);
ResultSet rs = pstmt.executeQuery();
i = 0;
while (rs.next())
{
student = new Student();
student.setSno(sno);
student.setSname(rs.getString(1));
student.setCxueqi(rs.getString(2));
student.setCname(rs.getString(3));
student.setCgrade(rs.getString(4));
i++;
list.add(student);
}
rs.close();
pstmt.close();
dbc.close();
} catch (Exception e)
{
e.printStackTrace();
}
return list;
}
}
//DataBaseConnection.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class DataBaseConnection
{
String DBDRIVER = "sun.jdbc.odbc.JdbcOdbcDriver" ;
String DBURL = "jdbc:odbc:student";
Connection conn = null ;
PreparedStatement pstmt = null ;
String sql = null ;
public DataBaseConnection() {
try{
Class.forName(DBDRIVER) ;
} catch(Exception e1) {
System.out.println("数据库驱动程序加载失败!!!") ;
}
try {
conn = DriverManager.getConnection(DBURL) ;
} catch(Exception e2)
{
System.out.println("数据库连接失败!!!") ;
}
}
public Connection getConnection() {
return this.conn ;
}
public void close()
{
try{
conn.close() ;
} catch(Exception e4)
{
System.out.println("数据库关闭失败!!!") ;
}
}
}
//TestStudent.java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class TestStudent extends JFrame implements ActionListener {
JTable table,table2;
Object a[][];
Object name[] = { "学号","姓名","学期","课程","成绩" };
JButton query, insert, update, delete;
JTextField sno, sname;
int rows = 1;
JPanel p;
TestStudent(String s) {
super(s);
query = new JButton("查询"); //查询用 query
insert = new JButton("添加"); //添加用insert
query.addActionListener(this);
insert.addActionListener(this);
sno = new JTextField(10);
a = new Object[15][5];
table = new JTable(a, name);
p = new JPanel();
p.add(new JLabel("学号"));
p.add(sno);
p.add(query);
p.add(insert);
getContentPane().add(p, BorderLayout.NORTH);
add(p, BorderLayout.NORTH);
update = new JButton("修改"); //修改用updata
delete = new JButton("删除"); //删除用delete
update.addActionListener(this);
delete.addActionListener(this);
sname = new JTextField(10);
p = new JPanel();
p.add(new JLabel("姓名"));
p.add(sname);
p.add(update);
p.add(delete);
getContentPane().add(p, BorderLayout.CENTER);
add(p, BorderLayout.CENTER);
/*
a = new Object[rows][3];
table2 = new JTable(a, name2);
getContentPane()
.add(new JScrollPane(table2), BorderLayout.SOUTH);
add(new JScrollPane(table2), BorderLayout.SOUTH);
setSize(425,800);
*/
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane()
.add(new JScrollPane(table), BorderLayout.SOUTH);
add(new JScrollPane(table), BorderLayout.SOUTH);
setSize(800,1000);
}
public void actionPerformed(ActionEvent e) {
Student student=new Student();
student.setSno(sno.getText());
int i = 0 ;
student.setSname(sname.getText());
//System.out.println(student.getSname());
if(e.getSource()==insert) {
try {
student.insert(student);
JOptionPane.showMessageDialog(null, "数据添加成功");
}
catch (Exception e1) {
e1.printStackTrace();
}
}
else if(e.getSource()==update)
{
try
{
student.update(student);
JOptionPane.showMessageDialog(null, "数据更新成功");
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
else if(e.getSource()==delete)
{
try
{
student.delete(student.getSno());
JOptionPane.showMessageDialog(null, "数据删除成功");
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
else if(e.getSource()==query)
{
try
{
List list=student.queryBySno(student.getSno());
for(int i1=0;i1<list.size();i1++)
{
Student temp=(Student) list.get(i1);
table.setValueAt(temp.getSno(), i1, 0);
table.setValueAt(temp.getSname(), i1, 1);
table.setValueAt(temp.getCxueqi(), i1, 2);
table.setValueAt(temp.getCname(), i1, 3);
table.setValueAt(temp.getCgrade(),i1, 4);
System.out.println(temp.getSno());
System.out.println(temp.getSname());
System.out.println(temp.getCxueqi());
System.out.println(temp.getCname());
System.out.println(temp.getCgrade());
}
JOptionPane.showMessageDialog(null, "数据查询成功");
//Student temp2=new Student();
//temp2=student.queryBySno(student.getSno());
//JOptionPane.showMessageDialog(null, "数据查询成功");
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
}
public static void main(String args[])
{
TestStudent ts = new TestStudent("学籍管理");
ts.pack();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询