当前位置 : 主页 > 编程语言 > c++ >

聊天程序数据路操作

来源:互联网 收集:自由互联 发布时间:2021-07-03
gistfile1.txt import java.sql.*;import javax.swing.JOptionPane;/* * 与数据库相关的操作,单独封装成类 */public class SQL { static Connection ct; static Statement st1, st2; static PreparedStatement ps; static ResultSet rs1, rs
gistfile1.txt
import java.sql.*;
import javax.swing.JOptionPane;
/*
 * 与数据库相关的操作,单独封装成类
 */

public class SQL {
    static Connection ct;
    static Statement st1, st2;
    static PreparedStatement ps;
    static ResultSet rs1, rs2;
    static String user, pwd;


    //将连接数据库的方法封装为一个方法
    public static void ConnectSQL() {
        //Connection ct=null;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载驱动
            ct = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;integratedSecurity=true;DatabaseName=JJ"); //得到连接
            System.out.println("已成功连接数据库...");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static boolean compare(String username, String pwd) throws SQLException {
        boolean m = false;
        String pa;
        String sql2 = "Select password from RegisterTable where number= " + username;
        ConnectSQL();
        System.out.println(username + "  " + pwd);
        st2 = ct.createStatement();
        System.out.println(st2);
        rs2 = st2.executeQuery(sql2);
        System.out.println(rs2);
        while (rs2.next()) {
            pa = rs2.getString(1).trim();
            System.out.println(pa);
            if (pa.equals(pwd)) {
                //pa = rs2.getString(1);
                //System.out.print("哎呀我去");
                m = true;
            }
        }
        return m;
    }


    public static void Register(int number, String name, String password, String sex, int age, String email) {
        //创建火箭车
        ConnectSQL();
        try {
            String sql3="insert into RegisterTable(number,name,password,sex,age,email) values(?,?,?,?,?,?)";
            //String sql4="insert into RegisterTable values(number,name,password,)";
            //ps=ct.prepareStatement(sql4);
            System.out.println(number+" "+name+" "+password+" "+sex+" "+age+" "+email);
            ps = ct.prepareStatement(sql3);
            ps.setInt(1, number);
            ps.setString(2, name);
            ps.setString(3, password);
            //ps.setString(4,password2);
            ps.setString(4, sex);
            ps.setInt(5, age);
            ps.setString(6, email);


            //执行
            int i = ps.executeUpdate();
            if (i == 1) {
                JOptionPane.showMessageDialog(null, "注册成功", "提示消息", JOptionPane.WARNING_MESSAGE);

            } else {
                JOptionPane.showMessageDialog(null, "注册失败", "提示消息", JOptionPane.ERROR_MESSAGE);
            }


        } catch (SQLException Error) {
            // TODO Auto-generated catch block
            Error.printStackTrace();
        }
    }

    //获取用户名
    public static String getUserName(String num) throws Exception
    {
        String sql4="Select name from RegisterTable where number="+num;
        String name="";
        ConnectSQL();
        st1=ct.createStatement();
        rs1=st1.executeQuery(sql4);
        while (rs1.next()) {
            name = rs1.getString(1).trim();
            System.out.println(name);
        }
        return name;
    }
    //  判断用户名是否存在
    public static boolean canRegister(String userNumber) throws Exception{
        String sql3 = "Select number from RegisterTable where number= "+userNumber;
        ConnectSQL();
        st1=ct.createStatement();
        rs1=st1.executeQuery(sql3);
        if(rs1.next()) {
            return false;
        }
        return true;
    }
}
网友评论