package MyDemo;
import java.sql.*;
public class Denlu {
public Denlu(){
}
public static int shanchu(String id)
{
long a=chaxun();
if(a==0)
{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ren","root","");
Statement stmt=conn.createStatement();
String sql="delete from tablename where UserID='"+Integer.parseInt(id)+"'";
stmt.executeUpdate(sql);
stmt.close();
conn.close();
return 1;
}
catch(Exception sqle){
System.err.println(sqle);
return 3;
}
}
else
return 2;
}
public static int zhucezh(String name,String sex,String kahao,String mima,String phone)
{try{
//1.注册驱动
String driverClassName = "com.mysql.jdbc.Driver";
Class.forName("com.mysql.jdbc.Driver");
//2.连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ren","root","");
Statement stmt=conn.createStatement();
String sql="insert into tablename values(?,?,?,?,?,?)";
PreparedStatement ps=(PreparedStatement) conn.prepareStatement(sql);
ps.setString(1,kahao);
ps.setString(2,name);
ps.setString(3,mima);
ps.setString(4,phone);
ps.setString(5,sex);
ps.setInt(6,0);
ps.executeUpdate();
ps.close();
stmt.close();
conn.close();
return 1;
}
catch(Exception sqle){
System.err.println(sqle);
return 3;
}
}
public static int chagai(String name,String sex,String phone)
{
try{
//1.注册驱动
String driverClassName = "com.mysql.jdbc.Driver";
Class.forName("com.mysql.jdbc.Driver");
//2.连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ren","root","");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from tablename where UserID='"+Integer.parseInt(a.Kahao.getText())+"'");
if(rs.next())
{
stmt.executeUpdate("Update tablename set Usersex='"+sex+"',Username='"+name+"',Userphone='"+phone+"' where UserID='"+Integer.parseInt(a.Kahao.getText())+"'");
rs.close();
stmt.close();
conn.close();
return 1;
}
}
catch(Exception sqle){
System.err.println(sqle);
return 3;
}
return 2;
}
public static int gaimi(String y,String x)
{
long p;
p=checkUser(a.Kahao.getText(),y);
if(p==1)
{
try{
//1.注册驱动
String driverClassName = "com.mysql.jdbc.Driver";
Class.forName("com.mysql.jdbc.Driver");
//2.连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ren","root","");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from tablename where UserID='"+Integer.parseInt(a.Kahao.getText())+"'");
if(rs.next())
{
stmt.executeUpdate("Update tablename set UserPW='"+x+"' where UserID='"+Integer.parseInt(a.Kahao.getText())+"'");
rs.close();
stmt.close();
conn.close();
return 1;
}
}
catch(Exception sqle){
System.err.println(sqle);
return 3;
}
}
return 2;
}
public static long chaxun()
{
long j;
try{
//1.注册驱动
String driverClassName = "com.mysql.jdbc.Driver";
Class.forName("com.mysql.jdbc.Driver");
//2.连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ren","root","");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from tablename where UserID='"+Integer.parseInt(a.Kahao.getText())+"'");
if(rs.next())
{
j=rs.getInt("Useryue");
rs.close();
stmt.close();
conn.close();
return j;
}
}
catch(Exception sqle){
System.err.println(sqle);
return -2;
}
return -2;
}
public static int qukuan(long k,long j)
{
try{
//1.注册驱动
String driverClassName = "com.mysql.jdbc.Driver";
Class.forName("com.mysql.jdbc.Driver");
//2.连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ren","root","");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from tablename where UserID='"+k+"'");
if(rs.next())
{ j=rs.getInt("Useryue")-j;
if(j<0)
return 2;
stmt.executeUpdate("Update tablename set Useryue='"+j+"' where UserID='"+k+"'");
rs.close();
stmt.close();
conn.close();
return 1;
}
}
catch(Exception sqle){
System.err.println(sqle);
return 3;
}
return 0;
}
public static int cunkuan(long k,long j)
{
try{
//1.注册驱动
String driverClassName = "com.mysql.jdbc.Driver";
Class.forName("com.mysql.jdbc.Driver");
//2.连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ren","root","");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from tablename where UserID='"+k+"'");
if(rs.next())
{ j=rs.getInt("Useryue")+j;
stmt.executeUpdate("Update tablename set Useryue='"+j+"' where UserID='"+k+"'");
rs.close();
stmt.close();
conn.close();
return 1;
}
}
catch(Exception sqle){
System.err.println(sqle);
return 2;
}
return 0;
}
public static int zhuanzhang(long k,long j)
{
long i = 0;
long p = 0;
i=Denlu.cunkuan(k, 0);
if(i==1)
{
p=Denlu.qukuan(Integer.parseInt(a.Kahao.getText()), j);
if(p==1)
{
try{
//1.注册驱动
String driverClassName = "com.mysql.jdbc.Driver";
Class.forName("com.mysql.jdbc.Driver");
//2.连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ren","root","");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from tablename where UserID='"+k+"'");
if(rs.next())
{ j=rs.getInt("Useryue")+j;
stmt.executeUpdate("Update tablename set Useryue='"+j+"' where UserID='"+k+"'");
rs.close();
stmt.close();
conn.close();
return 1;
}
}
catch(Exception sqle){
System.err.println(sqle);
return 4;
}
}
return 2;
}
return 3;
}
public static int checkUser(String k,String m)
{
try{
//1.注册驱动
String driverClassName = "com.mysql.jdbc.Driver";
Class.forName("com.mysql.jdbc.Driver");
//2.连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ren","root","");
String temp_ID=k;
String temp_pass=m;
String sql_user="select userID,userPW from tablename where UserID=? and UserPW=?";
PreparedStatement pst=null;
pst=conn.prepareStatement(sql_user);
pst.setString (1,temp_ID); //为绑定变量赋值
pst.setString (2,temp_pass);
ResultSet rst=pst.executeQuery();
if(rst.next())
{
rst.close();
pst.close();
conn.close();
return 1;
}
else
return 0;
}
catch(Exception sqle){
System.err.println(sqle);
return 0;
}
}
}