连接Mysql的简单注册
代码
//import java.sql.*;
import java.util.Scanner;
public class jdbcdl {
public static void main(String[]args) throws SQLException,ClassNotFoundException {
/*Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/testaaaaaaaa?useSSL=false","root","123456");
String sql = "update qasd set name= 'qas' where id = 1";
Statement statement = connection.createStatement();
int count = statement.executeUpdate(sql);
System.out.println(count);
statement.close();
connection.close();*/
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
register(username,password);
boolean flag = new jdbc_test().login(username,password);
if(flag){
System.out.println("注册成功!");
}else{
System.out.println("注册失败!");
}
}
public static void register(String username, String password) throws SQLException {
Connection conn = null;
Statement stmt = null;
try{
conn = jdbcutils.getConnection();
String sql = "Insert into qasd (name,password) values ('"+username+"','"+password+"')";
stmt = conn.createStatement();
stmt.executeUpdate(sql);
}catch (SQLException e){
e.printStackTrace();
}finally {
jdbcutils.close(stmt,conn);
}
}
}
运行结果