package fifteen;
import java.sql.*;
import java.util.Scanner;
public class Demo01<jdbc> {
public static final String DRIVER = "com.mysql.cj.jdbc.Driver";
public static final String URL="jdbc:mysql://localhost:3306/test?user=root&password=123456&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
public static void main(String[] args) {
System.out.println("请输入要登录的用户名与密码:");
Scanner sca= new Scanner(System.in);
String username = sca.nextLine();
String password = sca.nextLine();
Connection con=null;
PreparedStatement st=null;
ResultSet rs=null;
try {
Class.forName(DRIVER);
con = DriverManager.getConnection(URL);
String sql ="select * from t_login where username=? and password=?";
st= con.prepareStatement(sql);
st.setString(1, username);
st.setString(2, password);
System.out.println("登录" + (st.executeQuery().next() ? "成功" : "失败"));
System.out.print("显示如下:\n");
String findUsers = "select * from t_user";
st = con.prepareStatement(findUsers);
ResultSet result = st.executeQuery();
while (result.next()) {
String show = result.getInt("id") + " - " + result.getString("name") + " - " + (result.getInt("sex") == 1 ? "男" : "女") + " - " + result.getInt("birthday");
System.out.println(show);
}
System.out.print("请输入新增用户:\n");
String add= "insert into t_user(name,sex,birthday) values (?,?,?)";
st = con.prepareStatement(add);
st.setString(1, sca.nextLine());
st.setInt(2, "男".equals(sca.nextLine()) ? 1 : 0);
st.setInt(3, sca.nextInt());
st.execute();
System.out.println("添加成功");
result.close();
st.close();
con.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
![](https://img2018.cnblogs.com/i-beta/1791090/201912/1791090-20191215212355539-1033501068.png)
![](https://img2018.cnblogs.com/i-beta/1791090/201912/1791090-20191215211347976-944458319.png)