本文目錄一覽:
- 1、用JAVA代碼實現登錄名字與密碼
- 2、java語言實現用戶註冊和登錄
- 3、java 如何自動填充網頁上的用戶名和密碼?
- 4、用JAVA編寫一個用戶或註冊登錄界面。請哪位高手能夠寫下具體的代碼,謝謝
- 5、用JAVA編寫輸入用戶名和密碼
- 6、用java編程實現用戶註冊並進行登錄操作
用JAVA代碼實現登錄名字與密碼
1.寫一個操作數據庫的通用DAO(假設為Dao.java),提供
·加載數據庫驅動和獲取數據庫連接的方法:void getConnection();
·執行查詢的方法:ResultSet executeQuery(String sql);
·執行更新的方法:Integer executeUpdate(String sql);
·關閉資源的方法:void releaseResource(Connection con);
2.在你的界面中把用戶名和密碼取出來
·username = yourTextField.getText();
·password = yourPasswordField.getText();
3.匹配
·驗證username、password是否是有效輸入值(例如:不能包含特殊字符,不能有注入嫌疑等)
·String sql = “select * from yourTable where username='”+username+”‘ and password='”+password+”‘” ;
·Dao.executeQuery(sql):如果有結果說明合法,否則不合法。
不懂Connect 我
java語言實現用戶註冊和登錄
//這個是我寫的,裡面有連接數據庫的部分。你可以拿去參考一下
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
class LoginFrm extends JFrame implements ActionListener// throws Exception
{
JLabel lbl1 = new JLabel(“用戶名:”);
JLabel lbl2 = new JLabel(“密碼:”);
JTextField txt = new JTextField(5);
JPasswordField pf = new JPasswordField();
JButton btn1 = new JButton(“確定”);
JButton btn2 = new JButton(“取消”);
public LoginFrm() {
this.setTitle(“登陸”);
JPanel jp = (JPanel) this.getContentPane();
jp.setLayout(new GridLayout(3, 2, 5, 5));
jp.add(lbl1);
jp.add(txt);
jp.add(lbl2);
jp.add(pf);
jp.add(btn1);
jp.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btn1) {
try {
Class.forName(“com.mysql.jdbc.Driver”);// mysql數據庫
Connection con = DriverManager.getConnection(
“jdbc:mysql://localhost/Car_zl”, “root”, “1”);// 數據庫名為Car_zl,密碼為1
System.out.println(“com : “+ con);
Statement cmd = con.createStatement();
String sql = “select * from user where User_ID='”
+ txt.getText() + “‘ and User_ps='”
+ pf.getText() + “‘” ;
ResultSet rs = cmd
.executeQuery(sql);// 表名為user,user_ID和User_ps是存放用戶名和密碼的字段名
if (rs.next()) {
JOptionPane.showMessageDialog(null, “登陸成功!”);
} else
JOptionPane.showMessageDialog(null, “用戶名或密碼錯誤!”);
} catch (Exception ex) {
}
if (ae.getSource() == btn2) {
System.out.println(“1111111111111”);
//txt.setText(“”);
//pf.setText(“”);
System.exit(0);
}
}
}
public static void main(String arg[]) {
JFrame.setDefaultLookAndFeelDecorated(true);
LoginFrm frm = new LoginFrm();
frm.setSize(400, 200);
frm.setVisible(true);
}
}
java 如何自動填充網頁上的用戶名和密碼?
java 自動填充網頁上的用戶名和密碼有兩種方法:
方法一、在input 標籤裡面的 value屬性賦值即可,如:
input value=’x’ //這樣就每次打開都自動填充x
方法二、用java模擬post提交;代碼如下;
package com.sxzl.test;
public class TestRuntime {
public static void main(String args[]) {
try {
//String myArgs[] ={“
};
Process process = Runtime.getRuntime().exec(
//”cmd.exe /c start
“C:\\Program Files\\Internet Explorer\\iexplore.exe”
+”
);
} catch (Exception e) {
e.printStackTrace();
}
}
}
用JAVA編寫一個用戶或註冊登錄界面。請哪位高手能夠寫下具體的代碼,謝謝
效果圖
代碼
!DOCTYPE html
html
head
meta charset=”UTF-8″
title先鋒圖書館管理系統-登錄/title
style
*{
margin: 0;
padding: 0;
list-style: none;
}
#top{
width: 1000px;
height: 95px;
margin: 0 auto;
margin-top: 25px;
}
#top_top{
width: 1000px;
height: 65px;
background: deepskyblue;
}
#top_top_left{
width: 300px;
height: 65px;
float: left;
}
#top_top_leftlabel{
width: 200px;
height: 65px;
color: white;
float: right;
}
#top_top_left#a2{
padding-left: 10px;
padding-top: 20px;
font-size: 16px;
}
#top_bottom{
width: 1000px;
height: 30px;
}
#top_bottom_left{
width: 340px;
height: 30px;
line-height: 30px;
font-size: 12px;
background: skyblue;
color: white;
text-indent: 2em;
float: left;
}
#top_bottom_right{
width: 660px;
height: 30px;
line-height: 30px;
font-size: 12px;
color: blueviolet;
text-align: center;
float: right;
background: lightskyblue;
}
#content{
width: 1000px;
height: 600px;
margin: 0 auto;
background:#587FBA;
}
#content#text{
width: 1000px;
height: 50px;
line-height: 50px;
padding-top: 100px;
font-size: 36px;
font-family:”楷體”;
font-weight: bold;
text-align: center;
}
#content#login{
width: 480px;
height: 210px;
margin-top: 20px;
margin-left: 260px;
background: #85A0CB;
}
#content#loginimg{
float: left;
}
#content#login#select{
width: 305px;
height: 210px;
float: right;
}
#content#login#selectdiv{
width: 230;
height: 30px;
margin-left: 30px;
}
#content#login#select#d1{
margin-top:30px;
margin-bottom: 3px;
}
#content#login#selectp{
font-size: 14px;
margin-left: 95px;
}
#bottom{
width: 1000px;
height: 35px;
line-height: 35px;
margin: 0 auto;
background: deepskyblue;
text-align: center;
color: white;
}
/style
/head
body
div id=”top”
div id=”top_top”
div id=”top_top_left”
img src=”img/test/a13.png” width=”78px” height=”65px”label id=”a2″先鋒圖書館系統管理平台/label
/div
/div
div id=”top_bottom”
div id=”top_bottom_left”當前位置 : 首頁 系統管理 登錄/div
div id=”top_bottom_right”當前時間 : label id=”lable”/label/div
/div
/div
div id=”content”
div id=”text”歡迎登錄先鋒圖書館管理系統/div
div id=”login”
img src=”img/test/a14.png” width=”175px” height=”210px”/
form id=”select”
div id=”d1″用戶名: nbsp;nbsp;input type=”text” //div
div密 nbsp; 碼: nbsp;nbsp;input type=”password” //div
p
input type=”radio” name=”user” value=”read”/讀者nbsp;nbsp;nbsp;nbsp;
input type=”radio” name=”user” value=”admin”/管理員
/pbr/
p
input type=”button” value=”確定” style=”width: 50px;” onclick=”put()”/nbsp;nbsp;nbsp;nbsp;
input type=”reset” value=”重置” style=”width: 50px;”/
/p
/form
/div
/div
div id=”bottom”欣欣科技有限公司版權所有/div
/body
script type=”text/javascript” src=”JQuery/jquery.js”/script
script type=”text/javascript” src=”js/GetCurrentTime.js”/script
script
//驗證用戶名和密碼
function put(){
var d = $(“#selectdivinput”);//獲取用戶名和密碼
var name = d[0].value;
var pass = d[1].value;
var user = null;
var r = document.getElementsByName(“user”);//獲取用戶類型
for(i=0;ir.length;i++){
if(r[i].checked){
user=r[i].value;
}
}
//console.log(name + “,” +pass + “,” +user);//輸出測試
if(user==null){
window.alert(“請選擇用戶類型!”);
}else if(user==”admin” name!=”admin”){
window.alter(“用戶名錯誤!”);
}else if(user==”admin” name==”admin” pass!=”123456″){
window.alert(“密碼錯誤!”);
}else if(name==”admin” pass==”123456″ user==”admin”){
window.location.href=”work_02_welcome.html”;//在js中在本頁面中打開新鏈接
}else{
window.alert(“用戶名錯誤”);
}
}
/script
/html
用JAVA編寫輸入用戶名和密碼
import java.util.Scanner;
public class Logon {
private static final char username = ‘青’;
private static final int password = 123;
private static final Scanner san = new Scanner(System.in);
public static void main(String[] args) {
char iname;
int ipass;
System.out.print(“請輸入用戶名: “);
iname = san.next().charAt(0);
System.out.print(“請輸入密碼: “);
ipass = san.nextInt();
if(Logon.username == iname Logon.password == ipass) {
System.out.println(“歡迎你, ” + Logon.username);
} else {
System.out.println(“對不起, 你不是” + Logon.username);
}
}
}
用java編程實現用戶註冊並進行登錄操作
String username = “”,password = “”,passwordagain = “”; // 定義用戶名和密碼
將該變量等於為全局變量 或局部變量即可
原創文章,作者:UKKAL,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/317115.html