① java編程:怎麼在jsp頁面輸入數據的時候驗證資料庫里是否有同名的值吖
用戶名<input type = "text" id="username" onBlur="post()">
jquery+ajax:
function post() {
alert($("#username").val());
$.ajax({
type:"POST",
url:"user.action",//後台注冊方法,包含校驗或者直接校驗,按照自己的來
data: "user.name=" +$("#username").val(),
dataType: "html" ,
success:callback //回調函數
}) ;
}
function callback(data) {
var a = parseInt(data);
if(a == 0) {
alert("注冊成功");
}
else if(a==1) {
alert("該用戶名已經存在");
}
大概可以寫成這樣。。。哦了不
② java登錄查詢資料庫驗證
調用你已經創建專門的連接資料庫的類和返回查詢數據的類的方法獲取資料庫連接 然後執行sql就可以了
③ JAVA製作登陸框 連接資料庫驗證
import java.sql.*;
public class TestDB {
public static void main(String[] args) {
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; //驅動
String dbURL = "jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=user"; //資料庫名稱
String userName = "xuchibaba"; //你操作資料庫的用戶名
String userPwd = "123456"; //你操作資料庫的用戶名
Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
項目中要導入相應的資料庫驅動包 其中這段可能是你需要的
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; //驅動
String dbURL = "jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=user"; //資料庫名稱
String userName = "xuchibaba"; //你操作資料庫的用戶名
String userPwd = "123456"; //你操作資料庫的用戶名
Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!");
}
catch (Exception e) {
e.printStackTrace();
}
④ java如何驗證用戶名是否存在於資料庫的代碼
寫一個select語句,從資料庫中根據用戶名查找,就可以了
⑤ java鏈接mysql資料庫實現登陸如何驗證
//這是我以前寫的核對資料庫實現登陸的方法,你只看jdbc部分就好,我還特地給你加了點注釋
String sql = "select username,password from account";
String user = request.getParameter("user");
String pass = request.getParameter("password");
int j = 0;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCTools1.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
//從表中查詢獲取所有賬戶的用戶名&密碼的ResultSet 對象
while(rs.next()){
int i = 0;
String username[] = new String[10];//用戶名數組
String password[] = new String[10];//密碼數組
username[i] = rs.getString(1);
password[i] = rs.getString(2);
if(user.equals(username[i])&&pass.equals(password[i])){//比對
response.getWriter().print("you are welcome!");
j++;
}else if(user.equals(username[i])&&!pass.equals(password[i])){
response.getWriter().println("the realy password is :"+ username[i] +","+password[i]+"\r\n");
response.getWriter().println("and you password is :"+user +","+pass+" :so the username or password may not right");
j++;
}else{
continue;
}
i++;
}
if(j == 0){
response.getWriter().println("Your username may not be properly");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCTools1.release(rs, ps, conn);
}
//這是我JDBCTools的getConnection方法
getConnection{
String driverClass = oracle.jdbc.driver.OracleDriver;
String jdbcUrl = jdbc:oracle:thin:@localhost:1521:orcl;
//你的資料庫的用戶名密碼
String user = null;
String password = null;
// 通過反射創建Driver對象
Class.forName(driverClass);
return DriverManager.getConnection(jdbcUrl, user, password);}
//這是我JDBCTools的release方法
public static void release(ResultSet rs, Statement statement,
Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
⑥ 向資料庫插入的數據不重復,如何用Java做驗證
String
name
=
xx(這里寫你獲取到的准備插入的name的值)
select
name
from
(這里寫你的表名)
where
name
=
xx;
ResustSet
rs
=
(執行上句話生成結果集)
if(rs.next()){
(這里寫發現重復時的操作)
]
else{
(這里寫正式的插入語句)
}
⑦ java 驗證資料庫name是否存在
你的if寫反了吧,如果rs.next() 返回的是true的話說明記錄不空,那麼應該讓boadser = true
你這樣寫如果你用一個不存在的名字去運行程序估計就是true了。。。
⑧ java鏈接mysql資料庫實現登陸驗證
//這是我以前寫的核對資料庫實現登陸的方法,你只看jdbc部分就好,我還特地給你加了點注釋
String sql = "select username,password from account";
String user = request.getParameter("user");
String pass = request.getParameter("password");
int j = 0;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCTools1.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
//從表中查詢獲取所有賬戶的用戶名&密碼的ResultSet 對象
while(rs.next()){
int i = 0;
String username[] = new String[10];//用戶名數組
String password[] = new String[10];//密碼數組
username[i] = rs.getString(1);
password[i] = rs.getString(2);
if(user.equals(username[i])&&pass.equals(password[i])){//比對
response.getWriter().print("you are welcome!");
j++;
}else if(user.equals(username[i])&&!pass.equals(password[i])){
response.getWriter().println("the realy password is :"+ username[i] +","+password[i]+"\r\n");
response.getWriter().println("and you password is :"+user +","+pass+" :so the username or password may not right");
j++;
}else{
continue;
}
i++;
}
if(j == 0){
response.getWriter().println("Your username may not be properly");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCTools1.release(rs, ps, conn);
}
//這是我JDBCTools的getConnection方法
getConnection{
String driverClass = oracle.jdbc.driver.OracleDriver;
String jdbcUrl = jdbc:oracle:thin:@localhost:1521:orcl;
//你的資料庫的用戶名密碼
String user = null;
String password = null;
// 通過反射創建Driver對象
Class.forName(driverClass);
return DriverManager.getConnection(jdbcUrl, user, password);}
//這是我JDBCTools的release方法
public static void release(ResultSet rs, Statement statement,
Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}