package com.sky.jdbc;
import Utils.uitls;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
//用类注册数据库
public class Demo1 {
public static void main(String[] args) throws Exception {
User u = new User("zhang","123456","zhangsan","119","shenf",50);
int i = addUser(u);
if (i==1) {
System.out.println("注册成功");
System.out.println(u.toString());
}else {
System.out.println("注册失败");
}
}
public static int addUser(User user) throws Exception {
Connection conn = uitls.getCollection() ;
String sql = "insert into user values(null,?,?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,user.getUsername());
pstmt.setString(2,user.getPwd());
pstmt.setString(3,user.getName());
pstmt.setString(4,user.getPhone());
pstmt.setString(5,user.getCard());
pstmt.setInt(6,user.getMoney());
int i = pstmt.executeUpdate();
uitls.close(pstmt,conn);
return i;
}
}
用户类
package com.sky.jdbc;
public class User {
private String username;
private String pwd;
private String name;
private String phone;
private String card;
private int money;
public User() {
}
public User(String username, String pwd, String name, String phone, String card, int money) {
this.username = username;
this.pwd = pwd;
this.name = name;
this.phone = phone;
this.card = card;
this.money = money;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCard() {
return card;
}
public void setCard(String card) {
this.card = card;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", pwd='" + pwd + '\'' +
", name='" + name + '\'' +
", phone='" + phone + '\'' +
", card='" + card + '\'' +
", money=" + money +
'}';
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)