实验五_图形用户界面的设计与实现
- 格式:doc
- 大小:239.50 KB
- 文档页数:14
实验报告
课程名称 面向对象程序设计
实验名称 实验五 图形用户界面的设计与实现 日期 2017-11-9
学生学号 姓名 班级 B150404
实验目的:
通过本次实验,要求学生能进一步掌握图形用户界面的设计方法。
实验条件:
电脑一台、能上网查阅资料。
1.完成图书管理系统登陆界面和主界面的设计,完成图书管理系统的基本框架设计。
源代码:
数据库连接代码:
public class DbUtil {
private String dbUrl="jdbc:mysql://localhost:3306/db_book"; // 数据库连接地址
private String dbUserName="root"; // 用户名
private String dbPassword="123456"; // 密码
private String jdbcName="com.mysql.jdbc.Driver"; // 驱动名称
/**
* 获取数据库连接
* @return
* @throws Exception
*/
public Connection getCon()throws Exception{
Class.forName(jdbcName);
Connection con=DriverManager.getConnection(dbUrl, dbUserName,
dbPassword);
return con;
}
/**
* 关闭数据库连接
* @param con
* @throws Exception
*/
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 (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("数据库连接失败");
}
}
}
用户实体
/**
* 用户实体
* @author Administrator
*
*/
public class User {
private int id; // 编号
private String userName; // 用户名
private String password; // 密码
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String userName, String password) {
super();
erName = userName;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
erName = userName;
} public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
/**
* 用户Dao类
* @author Administrator
*
*/
public class UserDao {
/**
* 登录验证
* @param con
* @param user
* @return
* @throws Exception
*/
public User login(Connection con,User user)throws Exception{
User resultUser=null;
String sql="select * from t_user where userName=? and password=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
resultUser=new User();
resultUser.setId(rs.getInt("id"));
resultUser.setUserName(rs.getString("userName"));
resultUser.setPassword(rs.getString("password"));
}
return resultUser;
}
}
public class JavaInterFrm extends JInternalFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Java1234InterFrm frame = new Java1234InterFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Java1234InterFrm() {
getContentPane().setBackground(Color.RED);
setIconifiable(true);
setClosable(true);
setTitle("\u5173\u4E8EJava1234");
setBounds(100, 100, 450, 300);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new
ImageIcon(Java1234InterFrm.class.getResource("/images/java1234.png")));
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(98)
.addComponent(lblNewLabel)
.addContainerGap(126, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)