高分JAVA高手进~还会加分
最近才学JAVA~要做个学生信息增删改查~现在已经画好图咯~但是总是不晓得该怎样从文本框中传出值进SQL更不晓得杂将最后结果打印到画好的表格中数据库用的ACCESS名叫s...
最近才学JAVA~
要做个学生信息增删改查~
现在已经画好图咯~
但是总是不晓得该怎样从文本框中传出值进SQL
更不晓得杂将最后结果打印到画好的表格中
数据库用的ACCESS名叫student
表是~
student(sname,sno)
course(cname,cno,cxueqi)
sc(sno,cno,grade)
希望高手们给些提示~
还会加分的~
画图的代码是~Student.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 (String s){
super(s);
sele = new JButton("查询");
upde = new JButton("添加");
JTextField inputNumber1 = new JTextField("",10);
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);
getContentPane().add(p, BorderLayout.NORTH);
add(p,BorderLayout.NORTH);
inse = new JButton("修改");
dele = new JButton("删除");
JTextField inputNumber2 = new JTextField("",10);
p = new JPanel();
p.add(new JLabel("姓名"));
p.add(inputNumber2);
p.add(inse);
p.add(dele);
getContentPane().add(p, BorderLayout.CENTER);
add(p,BorderLayout.CENTER);
getContentPane().add(new JScrollPane(table), BorderLayout.SOUTH);
add(new JScrollPane(table),BorderLayout.SOUTH);
setSize(1000,800);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Student{
public static void main(String args[])
{
Win win = new Win("学籍管理");
}
} 展开
要做个学生信息增删改查~
现在已经画好图咯~
但是总是不晓得该怎样从文本框中传出值进SQL
更不晓得杂将最后结果打印到画好的表格中
数据库用的ACCESS名叫student
表是~
student(sname,sno)
course(cname,cno,cxueqi)
sc(sno,cno,grade)
希望高手们给些提示~
还会加分的~
画图的代码是~Student.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 (String s){
super(s);
sele = new JButton("查询");
upde = new JButton("添加");
JTextField inputNumber1 = new JTextField("",10);
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);
getContentPane().add(p, BorderLayout.NORTH);
add(p,BorderLayout.NORTH);
inse = new JButton("修改");
dele = new JButton("删除");
JTextField inputNumber2 = new JTextField("",10);
p = new JPanel();
p.add(new JLabel("姓名"));
p.add(inputNumber2);
p.add(inse);
p.add(dele);
getContentPane().add(p, BorderLayout.CENTER);
add(p,BorderLayout.CENTER);
getContentPane().add(new JScrollPane(table), BorderLayout.SOUTH);
add(new JScrollPane(table),BorderLayout.SOUTH);
setSize(1000,800);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Student{
public static void main(String args[])
{
Win win = new Win("学籍管理");
}
} 展开
6个回答
展开全部
先设置数据源,数据库名称为student 建好你的student表就可以拉
由于太长了,所以只给你做个student的增加\删除\修改\和查询显示到画好的表格中,自己慢慢体会然后再把其他两个做出来就可以拉,都是差不多的!!祝你好运!!
补充说明一点,那些注释用于调试用的!!
//TestStudent.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
public class TestStudent extends JFrame implements ActionListener
{
JTable table;
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[rows][3];
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);
getContentPane()
.add(new JScrollPane(table), BorderLayout.SOUTH);
add(new JScrollPane(table), BorderLayout.SOUTH);
setSize(1000, 800);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
Student student=new Student();
student.setSno(sno.getText());
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
{
Student temp=new Student();
temp=student.queryBySno(student.getSno());
table.setValueAt(temp.getSno(), 0, 0);
table.setValueAt(temp.getSname(), 0, 1);
System.out.println(temp.getSno());
System.out.println(temp.getSname());
JOptionPane.showMessageDialog(null, "数据查询成功");
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
}
public static void main(String args[])
{
TestStudent ts = new TestStudent("学籍管理");
ts.pack();
}
}
//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("数据库关闭失败!!!") ;
}
}
}
//Student.java
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Student
{
private String sno;
private String sname;
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 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 Student queryBySno(String sno)
{
Student student = null ;
String sql = "SELECT sname FROM student WHERE sno=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,sno) ;
ResultSet rs = pstmt.executeQuery() ;
if(rs.next())
{
student = new Student() ;
student.setSno(sno);
student.setSname(rs.getString(1)) ;
rs.close() ;
pstmt.close() ;
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
dbc.close() ;
}
return student ;
}
}
由于太长了,所以只给你做个student的增加\删除\修改\和查询显示到画好的表格中,自己慢慢体会然后再把其他两个做出来就可以拉,都是差不多的!!祝你好运!!
补充说明一点,那些注释用于调试用的!!
//TestStudent.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
public class TestStudent extends JFrame implements ActionListener
{
JTable table;
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[rows][3];
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);
getContentPane()
.add(new JScrollPane(table), BorderLayout.SOUTH);
add(new JScrollPane(table), BorderLayout.SOUTH);
setSize(1000, 800);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
Student student=new Student();
student.setSno(sno.getText());
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
{
Student temp=new Student();
temp=student.queryBySno(student.getSno());
table.setValueAt(temp.getSno(), 0, 0);
table.setValueAt(temp.getSname(), 0, 1);
System.out.println(temp.getSno());
System.out.println(temp.getSname());
JOptionPane.showMessageDialog(null, "数据查询成功");
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
}
public static void main(String args[])
{
TestStudent ts = new TestStudent("学籍管理");
ts.pack();
}
}
//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("数据库关闭失败!!!") ;
}
}
}
//Student.java
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Student
{
private String sno;
private String sname;
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 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 Student queryBySno(String sno)
{
Student student = null ;
String sql = "SELECT sname FROM student WHERE sno=?" ;
PreparedStatement pstmt = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
try
{
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,sno) ;
ResultSet rs = pstmt.executeQuery() ;
if(rs.next())
{
student = new Student() ;
student.setSno(sno);
student.setSname(rs.getString(1)) ;
rs.close() ;
pstmt.close() ;
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
dbc.close() ;
}
return student ;
}
}
展开全部
1、通过画面入力的值动态做成SQL文。如画面入力的分数是60分
做成SQL文可以是,SELECT * FROM STUDENT where 成绩 = "60"
如果是为了避免注入攻击,还可以使用预编译的方式,用JDBC的PrepareStatement.
2、检索结果的表示,如果表示的详细画面,比如某个学生的信息,直接使用JLabel就可以了。JLabel的setText(String content)可以把值设置到画面上,如果表示的是一览的信息则要用到JTable,具体的用法你可以到晚上找一下,应该比较多。
做成SQL文可以是,SELECT * FROM STUDENT where 成绩 = "60"
如果是为了避免注入攻击,还可以使用预编译的方式,用JDBC的PrepareStatement.
2、检索结果的表示,如果表示的详细画面,比如某个学生的信息,直接使用JLabel就可以了。JLabel的setText(String content)可以把值设置到画面上,如果表示的是一览的信息则要用到JTable,具体的用法你可以到晚上找一下,应该比较多。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先选用JDBC获取数据源,
然后调用相应的API把值给表.
从SWING组件中找相应的API(getValue)方法把值给
要调用的方法.
我以前用AWT和SWT做过一个考勤管理系统,后台也是ACCESS数据库.有什么问题提出来看看能不能帮你!
然后调用相应的API把值给表.
从SWING组件中找相应的API(getValue)方法把值给
要调用的方法.
我以前用AWT和SWT做过一个考勤管理系统,后台也是ACCESS数据库.有什么问题提出来看看能不能帮你!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
想要通过点击按钮来执行相应的操作,那就要在每个按钮上添加监听器,执行什么操作要在实现监听器时定义,至于数据库的连接,要用到JDBC技术连接对数据库的访问,一般用javaBean来实现。
相信我说的你很多都还没有学习到。有兴趣的话多问一下老师。或者在网查找相关的资料
相信我说的你很多都还没有学习到。有兴趣的话多问一下老师。或者在网查找相关的资料
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不用swing很多年...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询