java学生信息录入和查询连接mysql数据库文档格式.docx
《java学生信息录入和查询连接mysql数据库文档格式.docx》由会员分享,可在线阅读,更多相关《java学生信息录入和查询连接mysql数据库文档格式.docx(6页珍藏版)》请在冰豆网上搜索。
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{
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;
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("
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"
Ssex"
else{
Sage"
classSearchListenerimplementsActionListener{
ResultSetrs=null;
try{
3306/test"
//PreparedStatementps=conn.prepareStatement("
select*fromstudentwhere"
+slt+"
like'
%?
%'
Stringsql="
="
+jtfdInput.getText();
rs=stmt.executeQuery(sql);
}
System.out.println("
e2.printStackTrace();
}catch(SQLExceptione3){
e3.printStackTrace();
}catch(Exceptione4){
e4.printStackTrace();
{
try{
while(rs.next())
{
System.out.println(rs.getString
(1)+"
\t"
+rs.getString
(2)+"
+rs.getString(3)+"
+rs.getInt(4));
}
}catch(SQLExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}//while循环输出结果
}
}
Searchs=newSearch();
学生个人信息查询"
s.setSize(300,200);