注意:这个连接需要一个jar包,下载之后需要导入到自己的java项目加载,用于java连接数据库
地址:链接:https://pan.baidu.com/s/1yCxfo0iNvMa1P0xPqhakDg 提取码:1111
--创建数据库 create database jdbcmmsql use jdbcmmsql --创建表 create table user_t ( id int not null primary key, name varchar(20) not null, pwd varchar(32) not null, sex char(2), age int ) --插入数据 insert into user_t values (1,'123','123456','女',22)
package Gui.db; import java.sql.Connection; import java.sql.DriverManager; public class DbHelper { Connection conn = null; public DbHelper() { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //这里要根据自己的 库名,账号,密码连接。 conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=jdbcmmsql", "sa", "123456Mssql"); } catch (Exception e) { e.printStackTrace(); } } public Connection getConn() { return conn; } }
package Gui.entity; public class User { private int id; private String name; private String pwd; private String sex; private int age; public User() { } public User(int id, String name, String pwd, String sex, int age) { this.id = id; this.name = name; this.pwd = pwd; this.sex = sex; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package Gui.dao; import Gui.entity.User; import java.util.List; public interface UserDao { //增 public boolean addEmp(User emp); //删 public int delEmp(int id); //改 public int updEmp(User emp); //单查 public User findone(String user,String pwd); //查 public List<User> findAll(); }
只实现了一部分
package Gui.dao; import Gui.db.DbHelper; import Gui.entity.User; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class UserDaoImpl implements UserDao{ Connection conn; PreparedStatement ps; ResultSet rs; //增加 public boolean addEmp(User user) { boolean flag = false; conn = new DbHelper().getConn(); String sql = "insert into user_t(id,name,pwd,sex,age) values(?,?,?,?,?)"; try { ps = conn.prepareStatement(sql); ps.setInt(1, user.getId()); ps.setString(2, user.getName()); ps.setString(3, user.getSex()); ps.setInt(4, user.getAge()); ps.setInt(5, user.getAge()); if(ps.executeUpdate()>0) { flag = true; } } catch (SQLException e) { e.printStackTrace(); } return flag; } //删除 public int delEmp(int id) { return 0; } //修改 public int updEmp(User emp) { return 0; } //单查,可用于登录 public User findone(String usera, String userpwd) { User user_t = null; conn = new DbHelper().getConn(); String sql = "select name,pwd from user_t"; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ user_t = new User(); user_t.setName(rs.getString("name")); user_t.setPwd(rs.getString("pwd")); } } catch (SQLException e) { e.printStackTrace(); } return user_t; } //全查 public List<User> findAll() { List<User> ul = new ArrayList<User>(); User user = null; conn = new DbHelper().getConn(); String sql = "select * from user_t"; try { ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { user = new User(); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); user.setPwd(rs.getString("pwd")); user.setSex(rs.getString("sex")); user.setAge(rs.getInt("age")); ul.add(user); } } catch (SQLException e) { e.printStackTrace(); } return ul; } }
package Gui.Login; import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 登录成功打开此窗口,有一个案例小游戏 */ public class LoginYes { public LoginYes(){ JFrame f = new JFrame("你登陆成功了,来玩个小游戏吧