-1
I’m new to the programming world. With the help of some tutorials, I was able to build an application for school management.
Also with the help of the Javaplugados channel I was able to build a system that generates the license key and blocks when it expires as follows:
//Código que gera a senha
private void btnGerarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String data=txtVenc.getText();
int operacao=(Integer.parseInt(data)+132)/4;
txtDesblock.setText(String.valueOf(operacao));
}
// Código que valida a senha
private void jButtonValidarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mod.setCodigo(jTextFieldValidacao.getText());
controle.valida(mod.getCodigo());
System.exit(0);
}
private void jButtonSairActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
dispose();
}
//Controle da validação
package br.com.mtsoftware.telas;
import br.com.mtsoftware.dal.ModuloConexao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
/**
*
* @author Jelson Fernandes
*/
class ControleValidacao {
Connection conexao = null;
ResultSet rs = null;
//criando variaveis especiais para conexão com o banco
//prepared statement e resultSet são frameworks do pacote java.sql
//e servem para preparar eexecutar as instruções SQL
PreparedStatement pst = null;
int valida;
public void valida(String senha) {
conexao = ModuloConexao.conector();
try {
String sql = ("Select (data_vencimento)as vencimento from tbvencimento ");
// conexao.prepareStatement("Select (data_vencimento)as vencimento from tbvencimento ");
pst = conexao.prepareStatement(sql);
ResultSet rs = pst.executeQuery(sql);
// pst.executeQuery();
rs.last();
valida = Integer.parseInt(rs.getString("vencimento"));
int operacao = (valida + 132) / 4;//(data)+232/7
int senhaValidacao = Integer.parseInt(senha);
System.out.println("Operação" + operacao);
System.out.println("Senha" + senha);
if (operacao == senhaValidacao) {
int dia, mes, ano;
String AcertaMes = null, AcertaDia, ProxSenha;
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date hoje = new Date();
String data = df.format(hoje);
char[] senhachar = data.toCharArray();
dia = Integer.parseInt("" + senhachar[0] + senhachar[1]);
mes = Integer.parseInt("" + senhachar[3] + senhachar[4]);
ano = Integer.parseInt("" + senhachar[6] + senhachar[7] + senhachar[8] + senhachar[9]);
//JOptionPane.showMessageDialog(null, data);
// mes+=6;
// dia=15;
// mes+=1;
if (mes < 12) {
dia+=15;
// mes ++;
//ano++;
if (mes < 10) {
AcertaMes = "0" + mes;
} else {
AcertaMes = "" + mes;
}
} else {
mes=1;
// dia=15;
ano++;
AcertaMes = "0" + mes;
}
if (dia < 10) {
AcertaDia = "0" + dia;
} else {
AcertaDia = "" + dia;
}
ProxSenha = AcertaDia + AcertaMes + ano;
//String sql="insert into tbvencimento(data_vencime nto)value(?)";
//PreparedStatement pst = conexao.prepareStatement(sql);
pst = conexao.prepareStatement("insert into tbvencimento(data_vencimento)value(?)");
pst.setString(1, ProxSenha);
pst.execute();
// JOptionPane.showMessageDialog(null, ""+ProxSenha);
} else {
JOptionPane.showMessageDialog(null, "Senha Invalida. Verifique e Tente Novamente");
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Erro ao validar!\nERRO:" + ex);
}
}
}
//Sistema de Login com bloqueio assim que o sistema espira.
w java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
jPanel1.setBackground(new java.awt.Color(0, 0, 51));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("LOGIN"));
jPanel1.setForeground(new java.awt.Color(255, 255, 255));
jPasswordField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jPasswordFieldActionPerformed(evt);
}
});
jPasswordField.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jPasswordFieldKeyPressed(evt);
}
});
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Usuário");
jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setText("Senha");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(29, 29, 29)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jPasswordField)
.addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(29, 29, 29))
);
jLabelonoff.setBorder(new javax.swing.border.MatteBorder(null));
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mtsoftware/icones/LoginButton1.jpg"))); // NOI18N
jButton1.setBorder(new javax.swing.border.MatteBorder(null));
jButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel2.setBackground(new java.awt.Color(0, 204, 255));
jPanel2.setBorder(new javax.swing.border.MatteBorder(null));
jLabel4.setBackground(new java.awt.Color(204, 204, 204));
jLabel4.setFont(new java.awt.Font("RomanT", 1, 18)); // NOI18N
jLabel4.setForeground(new java.awt.Color(51, 0, 255));
jLabel4.setText("TOKES MULTI SOFT");
jLabel4.setBorder(new javax.swing.border.MatteBorder(null));
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mtsoftware/icones/LOGSOFT_Small-3.png"))); // NOI18N
jLabel3.setBorder(new javax.swing.border.MatteBorder(null));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(19, 19, 19))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(20, Short.MAX_VALUE)
.addComponent(jLabelonoff, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(227, 227, 227)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabelonoff, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void jPasswordFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
logar();
}
private void formWindowOpened(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
try {
String sql = ("Select (data_vencimento) from tbvencimento ");
pst = conexao.prepareStatement(sql);
ResultSet rs = pst.executeQuery(sql);
while (rs.next()) {
vencimento = rs.getString("data_vencimento");
// String dataSistemadataSistema.toString.valueOf(vencimento));
System.out.println(vencimento);
}
} catch (SQLException error) {
JOptionPane.showMessageDialog(null, " Erro de Consulta!\nERRO" + error);
}
}
private void jPasswordFieldKeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
try {
String sql = "select*from tbusuarios where login=? and senha=? ";
/*as linhas abaixo preparam a consulta em função do que foi inserido nas caixas de texto
o ? é substituido pelo coneudo das variaveis*/
pst = conexao.prepareStatement(sql);
System.out.println(conexao);
pst.setString(1, txtUsuario.getText());
pst.setString(2, jPasswordField.getText());
/* a linha abaixo executa a querry*/
//Result rs = stmt.executeQuery(sql);
rs = pst.executeQuery();
if (rs.next()) {
System.out.println();
//a linha abaixo obtem o conteudo do campo perfil da tabela tbusuario
String perfil = rs.getString(6);
// System.out.println(perfil);
//a estrutura abaixo faz o tratamento do perfil do usuário
pst = conexao.prepareStatement(sql);
rs.isLast();
SimpleDateFormat df = new SimpleDateFormat("ddMMyyyy");
Date hoje = new Date();
//JOptionPane.showMessageDialog(rootPane,df.format(hoje));
String dataAtual = (String) df.format(hoje);
//String dataSistema = rs.getString(vencimento);
String dataSistema = String.valueOf(vencimento);
System.out.println(dataAtual + "---" + dataSistema);
//JOptionPane.showMessageDialog(rootPane, dataSistema);
/* float valor1=(Float.parseFloat(dataSistema));
float valor2=(Float.parseFloat(dataAtual));
float valor3=(valor2-valor1);
System.out.println(valor3);
if(valor2-valor1<=15){
JOptionPane.showMessageDialog(null, "Faltam" +valor3 +"dias para este sistema expirar");
}*/
char[] dataAtualVet = dataAtual.toCharArray();
char[] dataSisVet = dataSistema.toCharArray();
int diaAt, mesAt, anoAt, diaVenc, mesVenc, anoVenc;
diaAt = Integer.parseInt("" + dataAtualVet[0] + dataAtualVet[1]);
mesAt = Integer.parseInt("" + dataAtualVet[2] + dataAtualVet[3]);
anoAt = Integer.parseInt("" + dataAtualVet[4] + dataAtualVet[5] + dataAtualVet[6] + dataAtualVet[7]);
diaVenc = Integer.parseInt("" + dataSisVet[0] + dataSisVet[1]);
mesVenc = Integer.parseInt("" + dataSisVet[2] + dataSisVet[3]);
anoVenc = Integer.parseInt("" + dataSisVet[4] + dataSisVet[5] + dataSisVet[6] + dataSisVet[7]);
// JOptionPane.showMessageDialog(rootPane, diaAt);
System.out.println(mesVenc);
Date data1 = df.parse(dataAtual);
Date data2 = df.parse(dataSistema);
long difference = data2.getTime() - data1.getTime();
float daysBetween = (difference / (1000 * 60 * 60 * 24));
if ((daysBetween >= 0)) {
switch (perfil) {
case "admin": {
TelaPrincipal principal = new TelaPrincipal();
principal.setVisible(true);
TelaPrincipal.jMenuBoletim.setEnabled(true);
TelaPrincipal.MenRel.setEnabled(true);
TelaPrincipal.menCadUsu.setEnabled(true);
TelaPrincipal.menPagamentos.setEnabled(true);
TelaPrincipal.lblUsuario.setForeground(Color.red);
TelaPrincipal.lblUsuario.setText(rs.getString(2));
AvaliacaoDoAluno avaliacao = new AvaliacaoDoAluno();
TelaPrincipal.jMenuItemPesquiVendas.setEnabled(true);
TelaPrincipal.jMenuItemFinancas.setEnabled(true);
// AvaliacaoDoAluno.jLabelUser.setText(rs.getString(2));
this.dispose();
break;
}
case "Programador": {
TelaPrincipal principal = new TelaPrincipal();
principal.setVisible(true);
TelaPrincipal.jMenuBoletim.setEnabled(true);
TelaPrincipal.MenRel.setEnabled(true);
TelaPrincipal.menCadUsu.setEnabled(true);
TelaPrincipal.menPagamentos.setEnabled(true);
TelaPrincipal.lblUsuario.setForeground(Color.red);
TelaPrincipal.lblUsuario.setText(rs.getString(2));
TelaPrincipal.jMenuItemPesquiVendas.setEnabled(true);
TelaPrincipal.jMenuItemFinancas.setEnabled(true);
TelaPrincipal.jMenuItemCadLicence.setEnabled(true);
TelaPrincipal.jMenuItemProgramador.setEnabled(true);
TelaPrincipal.jMenuItemProgramador.setEnabled(true);
AvaliacaoDoAluno avaliacao = new AvaliacaoDoAluno();
// AvaliacaoDoAluno.jLabelUser.setText(rs.getString(2));
this.dispose();
break;
}
case "Aluno": {
TelaPrincipal principal = new TelaPrincipal();
AvaliacaoDoAluno avaliacao = new AvaliacaoDoAluno();
principal.setVisible(true);
principal.setVisible(true);
TelaPrincipal.jMenuItemMensaldade.setEnabled(false);
TelaPrincipal.jMenuBoletim.setEnabled(false);
TelaPrincipal.menContratos.setEnabled(false);
TelaPrincipal.menPagamentos.setEnabled(false);
TelaPrincipal.menCadastro.setEnabled(false);
TelaPrincipal.menClasses.setEnabled(false);
TelaPrincipal.jMenuItemMensaldade.setEnabled(false);
AvaliacaoDoAluno.jButtonSalvar.setEnabled(false);
AvaliacaoDoAluno.jButtonAlterar.setEnabled(false);
AvaliacaoDoAluno.jButtonExcluir.setEnabled(false);
TelaPrincipal.lblUsuario.setText(rs.getString(2));
//AvaliacaoDoAluno.jLabelUsuario.setText(rs.getString(2));
TelaPrincipal.lblUsuario.setForeground(Color.green);
break;
}
default: {
TelaPrincipal principal = new TelaPrincipal();
principal.setVisible(true);
TelaPrincipal.lblUsuario.setText(rs.getString(2));
TelaPrincipal.lblUsuario.setForeground(Color.green);
this.dispose();
break;
}
}
} else if (perfil.equals("Programador")) {
TelaPrincipal principal = new TelaPrincipal();
principal.setVisible(true);
TelaPrincipal.jMenuBoletim.setEnabled(true);
TelaPrincipal.MenRel.setEnabled(true);
TelaPrincipal.menCadUsu.setEnabled(true);
TelaPrincipal.menPagamentos.setEnabled(true);
TelaPrincipal.lblUsuario.setForeground(Color.red);
TelaPrincipal.lblUsuario.setText(rs.getString(2));
TelaPrincipal.jMenuItemPesquiVendas.setEnabled(true);
TelaPrincipal.jMenuItemFinancas.setEnabled(true);
TelaPrincipal.jMenuItemCadLicence.setEnabled(true);
TelaPrincipal.jMenuItemProgramador.setEnabled(true);
TelaPrincipal.jMenuItemProgramador.setEnabled(true);
AvaliacaoDoAluno avaliacao = new AvaliacaoDoAluno();
// AvaliacaoDoAluno.jLabelUser.setText(rs.getString(2));
this.dispose();
} else {
TelaPrincipal principal = new TelaPrincipal();
// AvaliacaoDoAluno avaliacao = new AvaliacaoDoAluno();
// principal.setVisible(true);
principal.setVisible(true);
ValidacaoDoSistema valiadacao = new ValidacaoDoSistema();
valiadacao.setVisible(true);
Desktop.add(valiadacao);
TelaPrincipal.jMenuItemMensaldade.setEnabled(false);
TelaPrincipal.jMenuBoletim.setEnabled(false);
TelaPrincipal.menContratos.setEnabled(false);
TelaPrincipal.menPagamentos.setEnabled(false);
TelaPrincipal.menCadastro.setEnabled(false);
TelaPrincipal.menClasses.setEnabled(false);
TelaPrincipal.jMenuItemMensaldade.setEnabled(false);
TelaPrincipal.nenCadCli.setEnabled(false);
TelaPrincipal.menCadUsu.setEnabled(false);
TelaPrincipal.menCadEmpre.setEnabled(false);
TelaPrincipal.menFuncionarios.setEnabled(false);
TelaPrincipal.Escola.setEnabled(false);
TelaPrincipal.jMenuItemCadProdutos.setEnabled(false);
TelaPrincipal.jMenuItemVenda.setEnabled(false);
TelaPrincipal.jMenuItemBaixaVenda.setEnabled(false);
TelaPrincipal.jMenuItemPesquiVendas.setEnabled(false);
TelaPrincipal.jMenuItemFinancas.setEnabled(false);
}
this.dispose();
conexao.close();
} else {
JOptionPane.showMessageDialog(null, "usuário e//ou senha inválido(s)");
}
} catch (HeadlessException | SQLException e) {
JOptionPane.showMessageDialog(null, "Erro ao Logar!\nERRO" + e);
System.out.println(e);
} catch (ParseException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
}
So far it works properly. But when I change the system date, for example: the license expired on 08/30/2019 and change the system date to 07/30/2019 or another date before, it normally starts.
My question is this: You can count the validation days without depending on the system date?
OBS: For being insperient I would appreciate if you give me your answer a little more detailed and siples
Thank you Edson for the idea and for being so quick to respond. I will fight to intender and do
– Jelson Fernandes
The user would have a System with a problem when returning the date , making it unviable depending on the system, so this would be a reason for him not to do it , still you could get the date through the web , should have solution to this.
– Motta
@Motta this solution q I gave would not depend on the date, but on each "opening" of the system, each time q the system was "used", of course he would need to "force" this, but if he does not speak, no one will let a school management system open 24hours.
– Edson Filho
I agree , I just think that it has no problem because it returns the date of the machine is not a viable outline for the user to flee the license because the System becomes useless.
– Motta