package llxy.project.select;
import java.awt.Font;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class BySno {
public BySno(JTextField jt1,JTextArea jt){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "SELECT Sno,name,class,chinese,math,English From student";
//(1)加载并注册数据库驱动。
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
//(2)通过DriverManager获取数据库连接。
String url = "jdbc:mysql://localhost:3306/mytask";
String user ="root";
String password ="319224";
//(3)通过Connection对象获取Statement对象。
conn = DriverManager.getConnection(url, user,password);
//(4)使用Statement执行SQL语句。
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
//(5)操作结果集res
while(rs.next()){
//按学号查询
if(rs.getString(1).equals(jt1.getText())){
String str =(rs.getString("sno")+" "+rs.getString("name")+" "+rs.getString("class")+" "+rs.getString("chinese")+" "+rs.getString("math")+" "+rs.getString("English"));
String str1 =jt.getText();
String s = str1+"\n"+str;
//添加查询结果到文本域
jt.setText(s);
//设置字体的大小
jt.setFont(new Font(str, 0, 20));
}
}
} catch (SQLException e1) {
e1.printStackTrace();
} finally{
//(6)关闭连接,释放资源。
try {
conn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
JOptionPane.showMessageDialog(null, "查询到该学生信息");
}
}
package llxy.project.select;
import java.awt.Font;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ByClass {
public ByClass(JTextField jt3,JTextArea jt){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "SELECT Sno,name,class,chinese,math,English From student";
//(1)加载并注册数据库驱动。
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
//(2)通过DriverManager获取数据库连接。
String url = "jdbc:mysql://localhost:3306/mytask";
String user ="root";
String password ="319224";
//(3)通过Connection对象获取Statement对象。
conn = DriverManager.getConnection(url, user,password);
//(4)使用Statement执行SQL语句。
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
//(5)操作结果集res
while(rs.next()){
//按姓名查询
if(rs.getString(3).equals(jt3.getText())){
String str =(rs.getString("sno")+" "+rs.getString("name")+" "+rs.getString("class")+" "+rs.getString("chinese")+" "+rs.getString("math")+" "+rs.getString("English"));
String str1 =jt.getText();
String s = str1+"\n"+str;
//添加查询结果到文本域
jt.setText(s);
//设置字体的大小
jt.setFont(new Font(str, 0, 20));
}
}
} catch (SQLException e1) {
e1.printStackTrace();
} finally{
//(6)关闭连接,释放资源。
try {
conn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
JOptionPane.showMessageDialog(null, "查询到该学生信息");
}
}
使用姓名,课程查询的方式类似,只需要修改以上sql语句,即可。
package llxy.project.play;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import llxy.project.Tools.GuiTools;
public class AddStuMessage extends JFrame {
public static void main(String[] args) {
new AddStuMessage();
}
public AddStuMessage(){
this.setTitle("添加学生信息");
this.setLayout(new GridLayout(7,1));
this.setSize(500,400);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//添加界面
JLabel lsno = new JLabel("学号");
JLabel lname = new JLabel("姓名");
JLabel lclass = new JLabel("班级");
JLabel lchinese = new JLabel("语文");
JLabel lmath = new JLabel("数学");
JLabel lEnglish = new JLabel("英语");
//添加文本框
JTextField tsno = new JTextField(20);
JTextField tname = new JTextField(20);
JTextField tclass = new JTextField(20);
JTextField tchinese = new JTextField(20);
JTextField tmath = new JTextField(20);
JTextField tEnglish = new JTextField(20);
//添加按钮
JButton jb1 = new JButton("添加学生信息");
JButton jb2 = new JButton("退出");
//设置学号面板
JPanel jp1 = new JPanel();
jp1.add(lsno);
jp1.add(tsno);
jp1.setBackground(Color.LIGHT_GRAY);
//设置姓名面板
JPanel jp2 = new JPanel();
jp2.add(lname);
jp2.add(tname);
jp2.setBackground(Color.LIGHT_GRAY);
//设置班级面板
JPanel jp3 = new JPanel();
jp3.add(lclass);
jp3.add(tclass);
jp3.setBackground(Color.LIGHT_GRAY);
//设置课程面板
JPanel jp4 = new JPanel();
jp4.add(lchinese);
jp4.add(tchinese);
jp4.setBackground(Color.LIGHT_GRAY);
JPanel jp5 = new JPanel();
jp5.add(lmath);
jp5.add(tmath);
jp5.setBackground(Color.LIGHT_GRAY);
JPanel jp6 = new JPanel();
jp6.add(lEnglish);
jp6.add(tEnglish);
jp6.setBackground(Color.LIGHT_GRAY);
//Button按钮面板
JPanel jp7 = new JPanel();
jp7.add(jb1);
jp7.add(jb2);
jp7.setBackground(Color.lightGray);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.add(jp5);
this.add(jp6);
this.add(jp7);
GuiTools.center(this);
GuiTools.setTitleImage(this,"1.jpg");
this.setVisible(true);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Connection conn = null;
Statement stmt = null;
PreparedStatement ps =null;
String sql = "INSERT INTO student(sno,name,class,chinese,math,English)"+"values(?,?,?,?,?,?)";
//(1)加载并注册数据库驱动。
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//(2)通过DriverManager获取数据库连接。
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mytask","root","319224");
//(3)通过Connection对象获取Statement对象。
ps=conn.prepareStatement(sql);
ps.setString(1, tsno.getText());
ps.setString(2, tname.getText());
ps.setString(3, tclass.getText());
ps.setString(4, tchinese.getText());
ps.setString(5, tmath.getText());
ps.setString(6, tEnglish.getText());
//(4)使用PrepareStatement执行SQL语句。
ps.execute();
//(5)关闭连接,释放资源。
conn.close();
ps.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "成功添加学生信息");
dispose();
}
});
//返回按钮
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
}
package llxy.project.play;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import llxy.project.Tools.GuiTools;
public class DeleteStuMessage extends JFrame {
public static void main(String[] args) {
new DeleteStuMessage();
}
public DeleteStuMessage(){
this.setTitle("删除学生信息");
this.setSize(500,300);
this.setLayout(new GridLayout(2,1));
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JLabel lsno = new JLabel("学号");
JTextField tsno = new JTextField(18);
JButton jb1 = new JButton("删除学生信息");
JButton jb2 = new JButton("退出");
//删除信息面板
JPanel jp1 = new JPanel();
jp1.add(lsno);
jp1.add(tsno);
jp1.setBackground(Color.lightGray);
//按钮面板
JPanel jp2 = new JPanel();
jp2.add(jb1);
jp2.add(jb2);
this.add(jp1);
this.add(jp2);
jp2.setBackground(Color.lightGray);
GuiTools.center(this);
GuiTools.setTitleImage(this,"1.jpg");
this.setVisible(true);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String sno = tsno.getText(); //获取删除学生的学号
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
//(1)加载并注册数据库驱动。
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
//
try {
//(2)通过DriverManager获取数据库连接。
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mytask","root","319224");
stmt=conn.createStatement();
String sql = "DELETE FROM student WHERE sno='"+sno+"'";
stmt.executeUpdate(sql);
} catch (SQLException e1) {
e1.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
JOptionPane.showMessageDialog(null, "成功删除学生信息");
dispose();
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
}