package com.DB;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.spi.DirStateFactory.Result;
public class dbConnection {
String driverName="com.mysql.cj.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/12.8";
String user="root";
String password="1234";
public Connection getConnection(){
Connection con=null;
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
System.out.println("加载驱动失败");
}
try {
con=DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("数据库连接异常");
}
return con;
}
public void Close(Connection con) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("关闭数据库连接发生异常");
}
}
public void Close(PreparedStatement pre) {
try {
pre.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("关闭数据库操作资源发生异常");
}
}
public void Close(ResultSet rs) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("关闭数据库结果集发生异常");
}
}
}
文件结构
新建一个com.DB包,用来建立与数据库的连接,其中:
String driverName属性,为数据库添加驱动
String url属性,设置MySQL地址
String user:mysql用户名
String password:MySQL密码
定义Connect属性的方法,加载数据库驱动
定义关闭数据库连接的方法,重写方法
package com.Xiaoyi.JFrame;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.DB.dbConnection;
public class addStudentManger {
public void add(String name,String password,File file,float result) {
PreparedStatement pre=null;
//连接数据库
dbConnection dbconnection=new dbConnection();
Connection connection=dbconnection.getConnection();
String sql="insert into xydb values(?,?,?,?)";
try {
pre=connection.prepareStatement(sql);
pre.setString(1,name);
pre.setString(2,password);
pre.setFloat(4,result);
FileInputStream fil=new FileInputStream(file);//文件类转化文件数据流形式
pre.setBinaryStream(3, fil,file.length());;
pre.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(FileNotFoundException e) {
System.out.println("木有文件");
}
}
}
添加学生代码
preparedStatement对象:用来导入sql语句
其中,sql语句中的“?”为占位符,调用set方法,第一个参数为占位符的下标(从1开始),第二个参数为字段名,但file属性需要转化为数据流的形式,pre对象才客调用数据流方法加入参数
pre.executeUpdate:数据库更新方法
pre.executeQuery:数据库查询方法
点击按钮事件
话不多说,上代码
package com.Xiaoyi.JFrame;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class loginSwing extends JFrame implements ActionListener {
JLabel username=new JLabel("你叫嘛:");
JTextField jname=new JTextField(20);
JLabel passward=new JLabel("密码:");
JTextField jpass=new JTextField(20);
JLabel role=new JLabel("身份");
String[] roles={"学生","教师"};
JComboBox combobox=new JComboBox(roles);
JButton login=new JButton("登录");
JLabel space=new JLabel(" ");
public loginSwing() {
this.setTitle("登录页面");
this.setLayout(new FlowLayout());
this.setBounds(100, 100,310,200);
this.setVisible(true);
this.add(username);
this.add(jname);
this.add(passward);
this.add(jpass);
this.add(role);
this.add(combobox);
this.add(space);
this.add(login);
login.addActionListener(this);
}
public static void main(String[] args) {
new loginSwing();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==login) {
boolean flag=new loginManger().select(jname.getText(),jpass.getText());
String role=(String)combobox.getSelectedItem();
if(flag&&role.equals("教师")) {
new teacherSwing();
}else if(flag&&role.equals("学生")){
new StudentSwing(jname.getText());
}else{
System.out.println("用户名或密码错误");
}
}
}
}
想要实现点击按钮事件,需要继承监听器接口,并且实现它的抽象方法,想要让谁发生事件,就用它调用addActionListener方法
package com.Xiaoyi.JFrame;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.DB.dbConnection;
public class loginManger {
dbConnection dbc=new dbConnection();
//查询数据库有无要登录的用户
public boolean select(String name,String password) {
Connection connection=dbc.getConnection();
PreparedStatement pre=null;
ResultSet rs=null;
String sql="select*from xydb where name='" +name+"' and password='"+password+"'";
boolean flag=false;
//通过connection对象获取Statement对象
try {
pre=connection.prepareStatement(sql);
//使用Statement注销sql语句
rs=pre.executeQuery();
while(rs.next()) {
flag=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
dbc.Close(connection);
dbc.Close(pre);
dbc.Close(rs);
}
return flag;
}
}
定义布尔类型的方法,因为需要判断数据库中是否有此人,若有此人,方法返回true,反之,则返回false。connection.prepareStatement(sql);语句创建sql对象。调用executeQuery方法查询数据库中的内容,将其保存在resultSet rs结果集中。
之后就需要用到next()方法,具体介绍贴下来了
1、.next()方法的作用:指针指向下一条记录,有记录(有值)返回true并把记录内容存入到对应的对象中,也就是obj.next()的obj中。如果没有返回false。
2、.next()方法的应用:一般和ResultSet对象和while循环一起使用,去迭代结果集,并在循环中调用getXXX(intfieldIndex)/getXXX(String columnName)方法获取字段值。
过程:ResultSet对象具有指向其当前数据行的指针。开始,指针被置于第一行。.next()方法将指针移动到下一行,然后while循环迭代遍历ResultSet对象。
while (obj.next()) {
}
光标移动到下一行数据,有值(数据)返回true并迭代遍历,没有返回false退出循环。若直接返回true或false则不能灵活使用
22:41了,好困,碎觉碎觉