swing图形设计连接mysql数据库的步骤

来源:互联网 发布:淘宝退款不退货会怎样 编辑:程序博客网 时间:2024/05/19 00:36

工具:使用的是eclipse swing插件,mysql,mysql的图形管理器sequel pro

下面就是我列出来的操作步骤:


0、安装eclipse swing插件,详细过程可以参考:

https://www.cnblogs.com/yy3b2007com/p6741282.html

使用教程:自行查找,方便快捷!


1、创建一个java项目,在项目中导入java连接mysql的外部文件:

mysql-connector-java-5.1.44-bin.jar


2、首先在mysql的图形工具(或命令提示符)中建立所要连接的数据库(假设是database01,)


3、建立数据库的model包文件,里面要包含数据库database01的所有表(tables),例如以t_user表中包含id, username, password,

则在model这这个包中就要有User.java文件,文件中有基本的代码如下:

package model;


public class User {

privateintid;

private StringuserName;

private Stringpassword;

public User() {

super();

}

public User(StringuserName,Stringpassword) {

super();

this.userName =userName;

this.password =password;

}//构造方法,会在其他文件中用到!

public int getId() {

returnid;

}

public void setId(int id) {

this.id =id;

}

public String getUserName() {

returnuserName;

}

public void setUserName(String userName) {

this.userName =userName;

}

public String getPassword() {

returnpassword;

}

public void setPassword(String password) {

this.password =password;

}

}


4、我们再新建一个叫util的工具包文件,将连接mysql的工具类,例如叫dbutil.java,放入util包文件中。再连接其他数据库时就不用重复写相同的代码了,直接导入import util.dbutil;就行了!

dbutil.java要包含如下的代码:

package com.java.util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;//我忘了什么作用,你查下,好像是SQL异常类吧

/**

 * 链接mysql的工具类

 * @author 赵云龙

 *

 */

public class DbUtil {

private StringdbUrl ="jdbc:mysql://localhost/databasename";

private StringdbUserName ="root";

private StringdbPassword =“123456”;

private StringjdbcName ="com.mysql.jdbc.Driver";

//要连接的数据库名, 账号及密码,和驱动

/**

* 获得mysql链接

* @return

* @throws Exception

*/

public Connection getCon()throws Exception {

Class.forName(jdbcName);

Connection con = DriverManager.getConnection(dbUrl,dbUserName,dbPassword);

returncon;

}

//连接

public void closeCon(Connection con) throws Exception {

if(con!=null) {

con.close();

}

}

//测试代码

public static void main(String[] args) {

DbUtil dbUtil=new DbUtil();

try {

dbUtil.getCon();

System.out.println("数据库连接成功");

} catch (Exceptione) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


5、新建对数据库进行操作的dao包文件,例如对User对应的表t_user的操作,UserDao.java,这个文件中要导入User.java,使用import model.Use

示例代码如下:

package dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;


import com.java.model.User;//这个文件中要导入User.java



public class UserDao  {


/**

* 登录验证

* @param con

* @param user

* @return

* @throws Exception

*/

public User login(Connectioncon,Useruser)throws Exception{

User resultUser=null;

String sql="select * from t_user where userName=? and password=?”;//查询表中所有记录


PreparedStatementpstmt=con.prepareStatement(sql);

pstmt.setString(1,user.getUserName());

pstmt.setString(2,user.getPassword());

//这一步要弄明白!!!这是连接SQL的步骤中的其中几步

可以参考:http://m.blog.csdn.net/yujin753/article/details/41675787.html

ResultSet rs=pstmt.executeQuery();


if(rs.next()){

resultUser=new User();

resultUser.setUserName(rs.getString("userName"));

resultUser.setPassword(rs.getString("password"));

}

returnresultUser;

}

}

在这个文件中还可以添加增、删、查获、改的代码。


6、最后一步是建立图形界面,用图形界面中的按键来连接数据库中的表。

我们建立view包,这个包中包括所有我们设计的界面,例如用t_user中的记录来设计一个登录界面,当点击“登录”时,连接数据库中的t_user,查询用户名、密码是否正确

例如LogOnFrm.java

0.1、首先导入

import dao.UserDao;//对数据库中t_user的操作类

import model.User;//t_user表的model

import util.DbUtil;//数据库连接工具类

0.2、在文件中写下如下代码:

     DbUtil dbUtil = new DbUtil();

UserDao userDao =new UserDao();//用构造函数使用数据库连接类和对数据库的操作类

0.3、按键监听事件的代码:

JButton jb_logon =new JButton("登录");

//按键监听

jb_logon.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String userName =userNameTxt.getText();

String password =new String (passwordTxt.getPassword());

if(StringUtil.isEmpty(userName)) {

JOptionPane.showMessageDialog(null,"用户名不能为空!");

return;

}

if(StringUtil.isEmpty(password)) {

JOptionPane.showMessageDialog(null,"密码不能为空!");

return;

}

//StringUtil.isEmpty()是一个判空函数的封装,在这里没有介绍!



0.4、登录连接数据库的代码:

User user = new User(userName,password);//构造函数


try {

User currenUser =userDao.login(dbUtil.getCon(),user);

//调用userDao.java的login函数,实现数据库连接

//dbUtil.getCon()

if(currenUser!=null) {

setVisible(false);//当前窗口销毁


new MainFrm().setVisible(true);

//新窗口打开

//JOptionPane.showMessageDialog(null,"登录成功!");

}else {

JOptionPane.showMessageDialog(null,"用户名或密码错误!");

}

} catch (Exceptione1) {

e1.printStackTrace();

}

}

0.3和0.4放在同一个按键监听事件下,才实现了6、开头说明的功能!



主要的思想:文件分包、封装等。文件要清晰,每一个包包含一种功能!


LogOnFrm.java代码展示:

package com.java.view;


import java.awt.BorderLayout;

import java.awt.EventQueue;


import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.text.JTextComponent;


import com.java.dao.UserDao;

import com.java.model.User;

import com.java.util.DbUtil;

import com.java.util.StringUtil;


import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JTextField;

import javax.swing.JPasswordField;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.Font;

import javax.swing.ImageIcon;


public classLogOnFrmextends JFrame {

DbUtil dbUtil = new DbUtil();

UserDao userDao = new UserDao();


private JPanelcontentPane;

private JTextFielduserNameTxt;

private JPasswordFieldpasswordTxt;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

LogOnFrm frame =new LogOnFrm();

frame.setVisible(true);

} catch (Exceptione) {

e.printStackTrace();

}

}

});

}


/**

* Create the frame.

*/

public LogOnFrm() {

this.setLocationRelativeTo(null);//窗口在屏幕中间显示

setResizable(false);

setTitle("管理员登录");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 300);

contentPane =new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JLabel label =new JLabel("学生管理系统");

label.setFont(new Font("Monaco", Font.PLAIN, 18));

label.setBounds(168, 18, 176, 47);

contentPane.add(label);

JLabel label_1 =new JLabel("用户名:");

label_1.setBounds(71, 108, 72, 16);

contentPane.add(label_1);

JLabel label_2 =new JLabel(" 码:");

label_2.setBounds(71, 154, 72, 16);

contentPane.add(label_2);

JTextField userNameTxt =new JTextField();

userNameTxt.setBounds(156, 103, 188, 26);

contentPane.add(userNameTxt);

userNameTxt.setColumns(10);

JPasswordField passwordTxt =new JPasswordField();

passwordTxt.setBounds(156, 149, 188, 26);

contentPane.add(passwordTxt);

JButton jb_logon =new JButton("登录");

jb_logon.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String userName =userNameTxt.getText();

String password =new String (passwordTxt.getPassword());

if(StringUtil.isEmpty(userName)) {

JOptionPane.showMessageDialog(null,"用户名不能为空!");

return;

}

if(StringUtil.isEmpty(password)) {

JOptionPane.showMessageDialog(null,"密码不能为空!");

return;

}

User user =new User(userName,password);

try {

User currenUser =userDao.login(dbUtil.getCon(),user);

if(currenUser!=null) {

//this.dispose();//当前窗口销毁

//setDefaultCloseOperation(EXIT_ON_CLOSE);

setVisible(false);

new MainFrm().setVisible(true);

//JOptionPane.showMessageDialog(null,"登录成功!");

}else {

JOptionPane.showMessageDialog(null,"用户名或密码错误!");

}

} catch (Exceptione1) {

e1.printStackTrace();

}

}


private void dispose() {

// TODO Auto-generated method stub

}

});

jb_logon.setBounds(82, 206, 117, 29);

contentPane.add(jb_logon);

JButton jb_reset =new JButton("重置");

jb_reset.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

userNameTxt.setText("");  

passwordTxt.setText(""); 

}

});

jb_reset.setBounds(258, 206, 117, 29);

contentPane.add(jb_reset);

}


}

参考:java1234.com,不懂的可以到这个网站去学习。不是广告
原创粉丝点击