「java校验设计」java数据校验框架
今天给各位分享java校验设计的知识,其中也会对java数据校验框架进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java 方法参数怎么优雅校验
- 2、JAVA 设计swing登录界面验证用户名密码
- 3、java后台参数校验,比如是否是汉子、字母、数字的组合,这种校验出了正则表达式,还有别的方法吗?
- 4、使用Java如何验证所以日期是否正确
java 方法参数怎么优雅校验
建议参考validation框架,比较常用的实现 hibernate-validation,使用@annatation进行声明式校验,并且校验代码可以复用,同时约束应用框架的数据标准。
JAVA 设计swing登录界面验证用户名密码
应用的还是web的啊..
我给你写了个应用的哈
你在C盘建个test.txt文件
里面写
username:用户名(这里可以随便写哈)
password:同上哈
注意是要换行的哦。。
比如
username:tiger
password:tiger
然后你建个Login的类
然后把下面的代码弄进去
运行就是了
注意登陆的时候你文件里面设定的什么用户名和密码就输入什么哈
输入错误就会提示输入错误的
import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author thinkpad
*/
public class Login extends javax.swing.JFrame {
/** Creates new form Login */
private static String username;
private static String password;
public Login() {
initComponents();
try {
BufferedReader br = new BufferedReader(new FileReader("C:\\test.txt"));
username = br.readLine().split("\\:")[1];
password = br.readLine().split("\\:")[1];
System.out.println(username + password);
} catch(Exception e) {
e.printStackTrace();
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// editor-fold defaultstate="collapsed" desc="Generated Code"
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jPasswordField1 = new javax.swing.JPasswordField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jLabel1.setText("Login");
jLabel2.setText("Username:");
jLabel3.setText("Password:");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jPasswordField1ActionPerformed(evt);
}
});
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Reset");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(28, 28, 28)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(33, 33, 33)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jPasswordField1, 0, 0, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2))))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jLabel1)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)))
);
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()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(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()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// /editor-fold
private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.jTextField1.setText("");
this.jPasswordField1.setText("");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String inputUsername = this.jTextField1.getText();
String inputPassword = String.valueOf(this.jPasswordField1.getPassword());
if(inputUsername.equals(username) inputPassword.equals(password)) {
JOptionPane.showMessageDialog(this, "Login success!");
} else {
JOptionPane.showMessageDialog(this, "Login failed!");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
Login login = new Login();
login.setVisible(true);
login.setLocationRelativeTo(null);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
java后台参数校验,比如是否是汉子、字母、数字的组合,这种校验出了正则表达式,还有别的方法吗?
public class Test {
public static void main(String args[]) {
String a= "ddd122";//输出true,没中文
//a="我";//输出false,有中文
System.out.println(a.length()==a.getBytes().length);
}
}
这种方法可以判断是否含有中文。
使用Java如何验证所以日期是否正确
Java为了支持多语言,没有固定的日期格式。你需要根据自己的需要指定日期格式,然后用DateFormat类或者SimpleDateFormat类来判断是否是正确的日期格式。下面的例子供参考。更详细的内容(比如yyyy,MM,dd各代表什么)可以参考javadoc。
public class DateUtil
{
private static final SimpleDateFormat dateFormat = null;
static
{
// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
dateFormat = new SimpleDateFormat("yyyy/MM/dd");
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
dateFormat.setLenient(false);
}
public static boolean isValidDate(String s)
{
try
{
dateFormat.parse(s);
return true;
}
catch (Exception e)
{
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
return false;
}
}
// 下面这个方法则可以将一个日期按照你指定的格式输出
public static String formatDate(Date d)
{
return dateFormat.format(d);
}
}
java校验设计的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java数据校验框架、java校验设计的信息别忘了在本站进行查找喔。