📄 userdao.java
字号:
package com.zjy.struts.mypackage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.zjy.struts.form.UsersForm;
public class UserDao {
public PreparedStatement ps = null;
public Connection con = null;
public UserDao() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
.newInstance();
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=luntan";
String user = "sa";
String password = "";
con = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
// TODO: handle exception
}
}
public int insertUsers(UsersForm usersForm) {
UsersForm uf = usersForm;
int rs = 0;
Date t = new Date();
String ts = t.toLocaleString();
try {
ps = con
.prepareStatement("insert into users(name, password, adds, sex, email, age, times) values(?,?,?,?,?,?,?)");
ps.setString(1, bx(uf.getName()));
ps.setString(2, uf.getPassword());
ps.setString(3, bx(uf.getAdds()));
ps.setString(4, bx(uf.getSex()));
ps.setString(5, bx(uf.getEmail()));
ps.setString(6, uf.getAge());
ps.setString(7, ts);
rs = ps.executeUpdate();
System.out.print("insert right!");
} catch (Exception e) {
System.out.println(e);
rs = 0;
System.out.print("insert wrong!");
}// 关闭Jdbc
finally {
try {
ps.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return rs;
}
public int selectName(String name) {
Statement stmt = null;
ResultSet rs = null;
int rst = 0;
try {
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
String sql = "select * from users where name='" + bx(name) + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) {
rst = 1;
// System.out.print(rst);
}
} catch (Exception e) {
// TODO: handle exception
System.out.print(e);
} finally {
try {
rs.close();
stmt.close();
// con.close();
} catch (Exception e) {
// TODO: handle exception
}
}
return rst;
}
public UsersForm selectInf(String name) {
Statement stmt = null;
ResultSet rs = null;
UsersForm uf = new UsersForm();
try {
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
String sql = "select * from users where name='" + bx(name) + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) {
uf.setId(rs.getInt(1));
uf.setName(bx(name));
uf.setAdds(rs.getString(4));
uf.setSex(rs.getString(5));
uf.setEmail(rs.getString(6));
uf.setAge(rs.getString(7));
// System.out.print(rst);
}
} catch (Exception e) {
// TODO: handle exception
System.out.print(e);
} finally {
try {
rs.close();
stmt.close();
// con.close();
} catch (Exception e) {
// TODO: handle exception
}
}
return uf;
}
public int updateUsers(UsersForm usersForm) {
UsersForm uf = usersForm;
Statement stmt = null;
int rst = 0;
try {
stmt = con.createStatement();
String sql = "update users set password='" + uf.getPassword()
+ "',adds='" + uf.getAdds() + "',sex='" + bx(uf.getSex())
+ "',email='" + uf.getEmail() + "',age='" + uf.getAge()
+ "' where name='" + uf.getName() + "'";
rst=stmt.executeUpdate(sql);
System.out.print("insert right!");
} catch (Exception e) {
System.out.println(e);
rst = 0;
System.out.print("insert wrong!");
}// 关闭Jdbc
finally {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return rst;
}
public List selectAll (){
List list =new ArrayList();
Statement stmt = null;
ResultSet rs = null;
try {
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String sql="select * from users";
rs=stmt.executeQuery(sql);
while(rs.next()){
UsersForm uf = new UsersForm();
uf.setId(rs.getInt(1));
uf.setName(rs.getString(2));
uf.setAdds(rs.getString(3));
uf.setSex(rs.getString(4));
uf.setEmail(rs.getString(5));
uf.setAge(rs.getString(6));
list.add(uf);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public int deleteUser(String ids){
Statement stmt = null;
int rst=0;
try {
int id=Integer.parseInt(ids);
stmt=con.createStatement();
String sql="delete from users where id="+id;
rst=stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
rst=0;
e.printStackTrace();
}finally{
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return rst;
}
public String bx(String s) {
try {
byte b[] = s.getBytes("iso-8859-1");
s = new String(b);
} catch (Exception e) {
}
return s;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -