我试图使用jdbc在mysql中注册用户

时间:2016-10-18 11:30:11

标签: java mysql jdbc

我正在尝试注册管理员和其他用户,但我的代码不起作用。任何人都可以帮助我吗?这是我用来创建数据库和表格的代码。 这是完整代码的链接:http://s000.tinyupload.com/index.php?file_id=71664130076755188207

public static void main(String[]args)throws Exception {new  Aregistration();
createTable(); 
 }
//Table
public static void createTable() throws Exception{
    try{
        Connection conn = getConnection();
        PreparedStatement create = conn.prepareStatement("CREATE TABLE IF NOT EXISTS admin(id int NOT NULL AUTO_INCREMENT,first_name varchar(45),last_name varchar(45),email varchar(320),password varchar(50),PRIMARY KEY(id))");
        create.executeUpdate();
    }catch(Exception e){System.out.println(e);}
    finally {
        System.out.println("System Updated");};
}
//End of Table
//Database
public static Connection getConnection() throws Exception{

    try{
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/admin?useSSL=false";
        String username = "root";
        String password = "toor";
        Class.forName(driver);

        Connection conn = DriverManager.getConnection(url,username,password);
        System.out.println("Connected");
        return conn;
    }catch(Exception e){
        System.out.println(e);
    }
    return null;
}
}

https://postimg.org/image/ltgqcgqzp/
https://postimg.org/image/r65kqlew5/

1 个答案:

答案 0 :(得分:1)

yourhm你的代码完全应该做的...我不明白你不能插入useres atm因为你从来没有写过这个类......

喜欢这个:

public static void insertNewUser(String id,String first_name,String last_name, String email,String password){
connection conn = getConnection();
PreparedStatment create = conn.prepareStatement("Insert into admin Values ?,?,?,?,?" );
PreparedStatment.setString(1,id);
PreparedStatment.setString(2,first_name);
PreparedStatment.setString(3,last_name);
PreparedStatment.setString(4,email);
PreparedStatment.setString(5,password);
PreparedStatment.exectueUpdate();

}