07.26 用户注册 java mysql
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;
public class Register {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("--------------------用户注册------------------");
System.out.println("请输入用户名:");
String username = scanner.next();
System.out.println("请输入密码:");
String psw = scanner.next();
System.out.println("请输入手机号:");
String phone = scanner.next();
Connection conn = null;
PreparedStatement pstmt = null;
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "199702110";
String sql = "insert into user values(null,?,?,?) ";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, psw);
pstmt.setString(3, phone);
pstmt.executeUpdate();
System.out.println("注册成功!");
} catch (Exception e) {
System.out.println("注册失败!");
e.printStackTrace();
}finally {
try {
if(pstmt != null) {
pstmt.close();
}
if(conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}