java学生信息录入和查询连接mysql数据库.docx
- 文档编号:10690680
- 上传时间:2023-02-22
- 格式:DOCX
- 页数:7
- 大小:15.99KB
java学生信息录入和查询连接mysql数据库.docx
《java学生信息录入和查询连接mysql数据库.docx》由会员分享,可在线阅读,更多相关《java学生信息录入和查询连接mysql数据库.docx(7页珍藏版)》请在冰豆网上搜索。
java学生信息录入和查询连接mysql数据库
1.成绩录入
packagestudentManage;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjavax.swing.*;
publicclassStudentManageSystemextendsJFrame{
JTextFieldjtfdID=newJTextField(8);
JTextFieldjtfdName=newJTextField(128);
JTextFieldjtfdSex=newJTextField(128);
JTextFieldjtfdAge=newJTextField(8);
publicStudentManageSystem(){
JButtonjbtnEnter=newJButton("录入");
JButtonjbtnClear=newJButton("清零");
JLabeljlblID=newJLabel("学号");
JLabeljlblName=newJLabel("姓名");
JLabeljlblSex=newJLabel("性别");
JLabeljlblAge=newJLabel("年龄");
setLayout(newGridLayout(5,2,5,5));
add(jlblID);
add(jtfdID);
add(jlblName);
add(jtfdName);
add(jlblSex);
add(jtfdSex);
add(jlblAge);
add(jtfdAge);
add(jbtnEnter);
add(jbtnClear);
jbtnEnter.addActionListener(newEnterListener());
jbtnClear.addActionListener(newClearListener());
}
classEnterListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
Booleanrs=null;
try{
Class.forName("com.mysql.jdbc.Driver");
Connectionconn=DriverManager.getConnection("jdbc:
mysql:
//127.0.0.1:
3306/test?
useUnicode=true&characterEncoding=utf8","root1","root");
Statementstmt=conn.createStatement();
Stringsql="insertintostudentvalues('"+jtfdID.getText()+"','"+jtfdName.getText()+"','"+jtfdSex.getText()+"','"+jtfdAge.getText()+"')";
stmt.execute(sql);
jtfdID.setText("");
jtfdName.setText("");
jtfdSex.setText("");
jtfdAge.setText("");
}
catch(ClassNotFoundExceptione2){
System.out.println("Sorry,can'tfindtheDriver!
");
e2.printStackTrace();
}catch(SQLExceptione3){
e3.printStackTrace();
}catch(Exceptione4){
e4.printStackTrace();
}
}
}
classClearListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
jtfdID.setText("");
jtfdName.setText("");
jtfdSex.setText("");
jtfdAge.setText("");
}
}
publicstaticvoidmain(String[]args){
StudentManageSystems=newStudentManageSystem();
s.setTitle("学生个人信息录入");
s.setSize(400,300);
s.setLocationRelativeTo(null);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.setVisible(true);
}
}
2.成绩查询
importjava.awt.BorderLayout;
importjava.awt.FlowLayout;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjavax.swing.JButton;
importjavax.swing.JComboBox;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JTextField;
importcom.mysql.jdbc.PreparedStatement;
publicclassSearchextendsJFrame{
JComboBoxjcbSelect=newJComboBox();
JTextFieldjtfdInput=newJTextField();
JButtonjbtnSearch=newJButton("查询");
Stringslt=null;//确定下拉框所选的内容
publicSearch(){
jcbSelect.addItem("学号");
jcbSelect.addItem("姓名");
jcbSelect.addItem("性别");
jcbSelect.addItem("年龄");
JPaneljpl1=newJPanel(newGridLayout(2,1));
//setLayout(newGridLayout(2,1));
jpl1.add(jcbSelect);
jpl1.add(jtfdInput);
setLayout(newGridLayout(2,1));
add(jpl1);
add(jbtnSearch);
jbtnSearch.addActionListener(newSearchListener());
if(jcbSelect.getSelectedItem()=="学号"){
slt="Sno";
}
elseif(jcbSelect.getSelectedItem()=="姓名"){
slt="Sname";
}
elseif(jcbSelect.getSelectedItem()=="性别"){
slt="Ssex";
}
else{
slt="Sage";
}
}
classSearchListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
ResultSetrs=null;
try{
Class.forName("com.mysql.jdbc.Driver");
Connectionconn=DriverManager.getConnection("jdbc:
mysql:
//127.0.0.1:
3306/test","root1","root");
//PreparedStatementps=conn.prepareStatement("select*fromstudentwhere"+slt+"like'%?
%'");
Statementstmt=conn.createStatement();
Stringsql="select*fromstudentwhere"+slt+"="+jtfdInput.getText();
rs=stmt.executeQuery(sql);
}
catch(ClassNotFoundExceptione2){
System.out.println("Sorry,can'tfindtheDriver!
");
e2.printStackTrace();
}catch(SQLExceptione3){
e3.printStackTrace();
}catch(Exceptione4){
e4.printStackTrace();
}
{
try{
while(rs.next())
{
System.out.println(rs.getString
(1)+"\t"+rs.getString
(2)+"\t"+rs.getString(3)+"\t"+rs.getInt(4));
}
}catch(SQLExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}//while循环输出结果
}
}
}
publicstaticvoidmain(String[]args){
Searchs=newSearch();
s.setTitle("学生个人信息查询");
s.setSize(300,200);
s.setLocationRelativeTo(null);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.setVisible(true);
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 学生 信息 录入 查询 连接 mysql 数据库
![提示](https://static.bdocx.com/images/bang_tan.gif)