本篇文章给大家谈谈java和数据库实现用户登录,以及java和数据库实现用户登录区别对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、javaweb怎么连接访问数据库实现登录2、java中如何实现登录界面与数据库正确连接?3、java和数据库连接,实现登录功能4、java中有登录界面怎么连接上数据库登陆5、java链接mysql数据库实现登陆如何验证?6、用JAVA编写一个用户登陆界面,用户验证通过数据库实现
javaweb怎么连接访问数据库实现登录
前端页面传过来用户名和密码,然后java后端通过jdbc去连接数据库,查找相应的表,比较,对的就登陆成功。
java中如何实现登录界面与数据库正确连接?
使用JDBC进行数据库的增删改查操作1.下载Microsoft SQL Server 2005 JDBC 驱动包jar文件 将jar文件引入工程中2.封装数据库链接的获取和关闭操作import java.sql.*;public class BaseDao {
/**
* 数据库驱动类的字符串,完整的包名加类名 在工程中查看添加的jar文件 能看到这个类
*/
private static final String DRIVE = “com.microsoft.sqlserver.jdbc.SQLServerDriver”; /**
* 数据库连接地址
*
* DataBaseName=数据库名称 其它固定
*/
private static final String URL = “jdbc:sqlserver://www.easyaq.com:1433;DataBaseName=bbs”; /**
* 连接数据库的用户名
*/
private static final String USER = “sa”; /**
* 用户密码
*/
private static final String PASSWORD = “”; /**
* 获取连接 异常直接抛出 或者捕获后自定义异常信息再抛出
*/
public static Connection getConnection() throws Exception {
Class.forName(DRIVE);
return DriverManager.getConnection(URL, USER, PASSWORD);
} /**
* 关闭与数据库的连接 释放资源
*/
public static void closeAll(ResultSet resultSet, PreparedStatement pst,
Connection connection) throws Exception {
if (resultSet != null)
resultSet.close();
if (pst != null)
pst.close();
if (connection != null)
connection.close();
}}3.创建图书的实体类public class Book {
/**
* 数据库主键
*/
private Long id; /**
* 作者
*/
private String author; /**
* 书名
*/
private String name;
/**
* 默认构造
*
*/
public Book() {
}
/**
* 全字段构造
* @param id
* @param author
* @param name
*/
public Book(Long id, String author, String name) {
this.id = id;
this.author = author;
this.name = name;
}
/**
* 以下为读写属性的方法
* @return
*/
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.创建与图书表交互的工具类import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;public class BookDao {
/**
* 添加新书
*
* @param book 要添加入数据库的图书 作者 书名 必须给定
*/
public void addBook(Book book) throws Exception {
// 连接
Connection connection = null;
// 执行语句
PreparedStatement pst = null;
try {
connection = BaseDao.getConnection();
// 构造执行语句
String sql = “insert into book values(” + book.getAuthor() + “,”
+ book.getName() + “)”;
pst = connection.prepareStatement(sql);
pst.executeUpdate(); } catch (Exception e) {
// 抛出异常
throw e;
} finally {
// 无论是否异常 均关闭数据库
BaseDao.closeAll(null, pst, connection);
}
} /**
* 查询所有书籍列表
*/
public ListBook getBooks() throws Exception {
// 用于存放查寻结果的集合
ListBook books = new ArrayListBook();
// 连接
Connection connection = null;
// 执行语句
PreparedStatement pst = null;
// 查询结果
ResultSet resultSet = null;
try {
connection = BaseDao.getConnection();
// 构造查询语句
String sql = “select * from book”;
pst = connection.prepareStatement(sql);
resultSet = pst.executeQuery(); // 循环读取查询结果行
while (resultSet.next()) {
// getXXX的参数为数据表列名
Book book = new Book(resultSet.getLong(“id”), resultSet
.getString(“author”), resultSet.getString(“name”));
// 将封装好的图书对象存入集合
books.add(book);
}
} catch (Exception e) {
// 抛出异常
throw e;
} finally {
// 无论是否异常 均关闭数据库
BaseDao.closeAll(resultSet, pst, connection);
}
// 返回查询结果
return books;
}/***其它方法类似上面 只是语句不同*/
}当然 以上只是简单的封装 初学者可以在理解以上代码的基础上 进行更高级的封装
5.使用BookDao添加书籍和获取所有书籍列表import java.util.List;/**
* 测试类
* @author Administrator
*
*/
public class Test { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//创建工具类对象
BookDao dao = new BookDao();
//创建一本图书
Book book = new Book(null,”QQ:495691293″,”编程菜鸟”);
//添加书籍到数据库
dao.addBook(book);
//获取所有图书列表
ListBook books = dao.getBooks();
//输出结果
for (Book b : books) {
System.out.println(b.getId()+”\t”+b.getAuthor()+”\t”+b.getName());
}
}}
java和数据库连接,实现登录功能
//验证用户和密码
@Override
public int queryUser(UserVo vo) {
// TODO Auto-generated method stub
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String sql = “select count(*) from user_info where username=? and password=?”;
int count = 0;
try{
//获取连接对像
conn = ConnTools.getConnection();
//获取stmt
stmt = conn.prepareStatement( sql );
//设置动态参数
stmt.setString( 1, vo.getUsername() );
stmt.setString( 2, vo.getPassword() );
//执行查询
rs = stmt.executeQuery();
if( rs.next() ){
count = rs.getInt( 1 );
}
}catch( Exception e ){
e.printStackTrace();
}finally{
//释放资源
ConnTools.close(conn, stmt, rs);
}
return count;
}
java中有登录界面怎么连接上数据库登陆
java中使用jdbc连接sql server数据库步骤:
1.JDBC连接SQL Server的驱动安装 ,前两个是属于数据库软件,正常安装即可(注意数据库登陆不要使用windows验证)
1 将JDBC解压缩到任意位置,比如解压到C盘program files下面,并在安装目录里找到sqljdbc.jar文件,得到其路径开始配置环境变量
在环境变量classpath 后面追加 C:\Program Files\Microsoft SQL Server2005 JDBC Driver\sqljdbc_1.2\enu\sqljdbc.jar
2 设置SQLEXPRESS服务器:
a.打开SQL Server Configuration Manager – SQLEXPRESS的协议 – TCP/IP
b.右键单击启动TCP/IP
c.双击进入属性,把IP地址中的IP all中的TCP端口设置为1433
d.重新启动SQL Server 2005服务中的SQLEXPRESS服务器
e.关闭SQL Server Configuration Manager
3 打开 SQL Server Management Studio,连接SQLEXPRESS服务器, 新建数据库,起名字为sample
4 打开Eclipse
a.新建工程- Java – Java project,起名为Test
b.选择eclipse-窗口-首选项-Java-installed JRE 编辑已经安装好的jdk,查找目录添加sqljdbc.jar
c.右键单击目录窗口中的Test, 选择Build Path -Configure Build Path…, 添加扩展jar文件,即把sqljdbc.jar添加到其中
5 编写Java代码来测试JDBC连接SQL Server数据库
import java.sql.*;
public class Test {
public static void main(String[] srg) {
//加载JDBC驱动
String driverName = “com.microsoft.sqlserver.jdbc.SQLServerDriver”;
//连接服务器和数据库sample
String dbURL = “jdbc:sqlserver://www.easyaq.com:1433; DatabaseName=sample”;
String userName = “sa”; //默认用户名
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();
}
}
}
执行以后就可以连接到sample数据库了。
通过上面方法就连接上了,连接上以后就可以进行相关操作了
java链接mysql数据库实现登陆如何验证?
//这是我以前写的核对数据库实现登陆的方法,你只看jdbc部分就好,我还特地给你加了点注释\x0d\x0aString sql = “select username,password from account”;\x0d\x0aString user = request.getParameter(“user”);\x0d\x0aString pass = request.getParameter(“password”);\x0d\x0aint j = 0;\x0d\x0aConnection conn = null;\x0d\x0aPreparedStatement ps = null;\x0d\x0aResultSet rs = null;\x0d\x0atry {\x0d\x0aconn = JDBCTools1.getConnection();\x0d\x0aps = conn.prepareStatement(sql);\x0d\x0ars = ps.executeQuery();\x0d\x0a//从表中查询获取所有账户的用户名密码的ResultSet 对象\x0d\x0awhile(rs.next()){\x0d\x0aint i = 0;\x0d\x0a\x0d\x0aString username[] = new String[10];//用户名数组\x0d\x0aString password[] = new String[10];//密码数组\x0d\x0ausername[i] = rs.getString(1);\x0d\x0apassword[i] = rs.getString(2);\x0d\x0aif(user.equals(username[i])pass.equals(password[i])){//比对\x0d\x0aresponse.getWriter().print(“you are welcome!”);\x0d\x0aj++;\x0d\x0a}else if(user.equals(username[i])!pass.equals(password[i])){\x0d\x0aresponse.getWriter().println(“the realy password is :”+ username[i] +”,”+password[i]+”\r\n”);\x0d\x0aresponse.getWriter().println(“and you password is :”+user +”,”+pass+” :so the username or password may not right”);\x0d\x0aj++;\x0d\x0a}else{\x0d\x0acontinue;\x0d\x0a}\x0d\x0ai++;\x0d\x0a}\x0d\x0aif(j == 0){\x0d\x0aresponse.getWriter().println(“Your username may not be properly”);\x0d\x0a}\x0d\x0a} catch (Exception e) {\x0d\x0ae.printStackTrace();\x0d\x0a}finally{\x0d\x0aJDBCTools1.release(rs, ps, conn);\x0d\x0a}\x0d\x0a//这是我JDBCTools的getConnection方法\x0d\x0agetConnection{\x0d\x0aString driverClass = oracle.jdbc.driver.OracleDriver;\x0d\x0aString jdbcUrl = jdbc:oracle:thin:@www.easyaq.com:1521:orcl;\x0d\x0a//你的数据库的用户名密码\x0d\x0aString user = null;\x0d\x0aString password = null;\x0d\x0a// 通过反射创建Driver对象\x0d\x0aClass.forName(driverClass);\x0d\x0areturn DriverManager.getConnection(jdbcUrl, user, password);}\x0d\x0a//这是我JDBCTools的release方法\x0d\x0apublic static void release(ResultSet rs, Statement statement,\x0d\x0aConnection conn) {\x0d\x0aif (rs != null) {\x0d\x0atry {\x0d\x0ars.close();\x0d\x0a} catch (SQLException e) {\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0aif (statement != null) {\x0d\x0atry {\x0d\x0astatement.close();\x0d\x0a} catch (Exception e2) {\x0d\x0ae2.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0aif (conn != null) {\x0d\x0atry {\x0d\x0aconn.close();\x0d\x0a} catch (Exception e2) {\x0d\x0ae2.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a}
用JAVA编写一个用户登陆界面,用户验证通过数据库实现
这是我以前写的系统登录框的login
package java1;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Rectangle;
import java.awt.Font;
public class login extends JFrame
{
public login() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args)
{
login frm=new login();
frm.setBounds(300,200,300,260);
frm.setVisible(true);
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
jLabel1.setForeground(Color.red);
jLabel1.setText(“用户名”);
jLabel1.setBounds(new Rectangle(25, 72, 68, 29));
jButton2.setBounds(new Rectangle(142, 159, 81, 30));
jButton2.setText(“取消”);
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jButton1.setBounds(new Rectangle(38, 159, 81, 29));
jButton1.setText(“确定”);
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jTextField1.setBounds(new Rectangle(109, 79, 136, 23));
jPasswordField1.setBounds(new Rectangle(109, 115, 136, 23));
this.getContentPane().add(jLabel1);
jLabel3.setFont(new java.awt.Font(“隶书”, Font.PLAIN, 24));
jLabel3.setText(“学 生 考 试 系 统”);
jLabel3.setBounds(new Rectangle(22, 15, 268, 47));
this.getContentPane().add(jTextField1);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jButton2);
this.getContentPane().add(jButton1);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jPasswordField1);
jLabel2.setForeground(Color.red);
jLabel2.setText(“密码”);
jLabel2.setBounds(new Rectangle(26, 112, 68, 29));
}
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField jTextField1 = new JTextField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JLabel jLabel3 = new JLabel();
JPasswordField jPasswordField1 = new JPasswordField();
main2 wo=new main2();
int sum;
//判断权限的方法
public void success()
{
try{
ResultSet rs1;
String s1=”select * from 管理权限 where name='”+jTextField1.getText()+”‘ and password='”+jPasswordField1.getText()+”‘”;
rs1=sqlcx.Rs_jiluji(s1);
rs1.first();
sum=Integer.parseInt((String)(rs1.getString(3)));
System.out.println(sum);
very();
}
catch(Exception c){System.out.println(“success error”);}
}
//根据权限设置窗口
public void very()
{
if (sum==1)
{JOptionPane.showOptionDialog(this, “您现在是以教师权限登陆”, “登陆信息”
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
else
{
JOptionPane.showOptionDialog(this, “你现在是以学生权限登陆”, “登陆信息”
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
wo.jButton2.setVisible(false);
wo.jButton3.setVisible(false);
wo.jButton4.setVisible(false);
wo.jButton5.setVisible(false);
wo.jButton6.setVisible(false);
wo.jButton7.setVisible(false);
wo.jButton8.setVisible(false);
wo.jMenu2.setVisible(false);
wo.jMenu3.setVisible(false);
wo.jMenu4.setVisible(false);
}
}
public void jButton1_actionPerformed(ActionEvent e)
{
//判断用户名是否为空
if (jTextField1.getText().length() == 0) {
JOptionPane.showOptionDialog(this, “用户名不能为空”, “错误信息”
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
//判断密码是否为空
else if (jPasswordField1.getText().length()==0)
{
JOptionPane.showOptionDialog(this,”密码不能为空”, “错误信息”
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
else
{
try
{
ResultSet rs;//声明记录集
String sql=”select * from 管理权限 where name='”+jTextField1.getText()+”‘”;
rs = sqlcx.Rs_jiluji(sql);
if (rs.next())
{
if(jPasswordField1.getText().equals(rs.getString(2)))
{
success();
wo.setBounds(60,40,700,600);
wo.setVisible(true);
this.dispose();
}
else
{
JOptionPane.showOptionDialog(this,”密码错误”, “错误信息”
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
}
else
{
JOptionPane.showOptionDialog(this,”没有所要找的用户名”, “错误信息”
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
}
catch(Exception c){
JOptionPane.showOptionDialog(this,”连接数据库失败”, “错误信息”
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
}
}
public void jButton2_actionPerformed(ActionEvent e) {
System.exit(0);
}
}
java和数据库实现用户登录的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java和数据库实现用户登录区别、java和数据库实现用户登录的信息别忘了在本站进行查找喔。