第15次作业
题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。
import java.sql.*;
import java.util.Scanner;
public class text7 {
public static void main(String[] args) {
Connection con=null;
Statement st=null;
boolean rs;
ResultSet rt=null;
Scanner sc=new Scanner(System.in);
System.out.println("请输入用户名");
String username=sc.next();
System.out.println("请输入密码");
String password=sc.next();
String sql="select * from login where username='"+username+"'and password='"+password+"'";
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/dbfjd";
con=DriverManager.getConnection(url,"root","123456");
st=con.createStatement();
rs=st.execute(sql);
if(username.equalsIgnoreCase(username)&&password.equalsIgnoreCase(password)){
System.out.println("登陆成功");
rt=DB.getInstance().executeSelect("select * from user", args);
while(rt.next()){
System.out.println("生日"+rt.getString(2)+"性别"+rt.getString(3)+"姓名"+rt.getString(4));
}
else{
System.out.println("登陆失败");
import java.util.Scanner;
public class text7 {
public static void main(String[] args) {
Connection con=null;
Statement st=null;
boolean rs;
ResultSet rt=null;
Scanner sc=new Scanner(System.in);
System.out.println("请输入用户名");
String username=sc.next();
System.out.println("请输入密码");
String password=sc.next();
String sql="select * from login where username='"+username+"'and password='"+password+"'";
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/dbfjd";
con=DriverManager.getConnection(url,"root","123456");
st=con.createStatement();
rs=st.execute(sql);
if(username.equalsIgnoreCase(username)&&password.equalsIgnoreCase(password)){
System.out.println("登陆成功");
rt=DB.getInstance().executeSelect("select * from user", args);
while(rt.next()){
System.out.println("生日"+rt.getString(2)+"性别"+rt.getString(3)+"姓名"+rt.getString(4));
}
else{
System.out.println("登陆失败");
}
}
}
}
}
题目2:在上一题基础上,当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示(要求使用DB.java完成登录和获取t_user表中数据的操作),最后再对t_user表进行一条记录的添加操作。
package demo; import java.sql.*; import java.util.Scanner; public class test { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan=new Scanner(System.in); String username=scan.next(); String password=scan.next(); Connection con=null; Statement st=null; ResultSet rs=null; ResultSet rs1=null; try { DriverManager.registerDriver(new com.mysql.jdbc.Driver()); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","082256"); st=con.createStatement(); String sql = "select * from t_login where username='" + username+ "' and password = '" + password + "'"; rs=st.executeQuery(sql); boolean bl=rs.next(); if(bl) { System.out.println("登陆成功"); rs1= DB.getInstance().executeSelect("select * from t_user", args); while(rs1.next()) { int id = rs1.getInt(1); String name = rs1.getString(2); String sex = rs1.getString(3); String birthday = rs1.getString(4); System.out.println("用户 名字 性别 生日"); System.out.println(id + " " + name + " " + sex + " " + birthday); } System.out.println("添加信息"); int id=scan.nextInt(); String name=scan.next(); String sex=scan.next(); String birthday=scan.next(); String sql1="insert into t_user(id,name,sex,birthday) values('"+id+"','"+name+"','"+sex+"','"+birthday+"')"; int count=DB.getInstance().executeModify(sql1, args); if(count>0) System.out.println("数据添加成功"); else System.out.println("数据添加失败"); } else { System.out.println("登陆失败"); } } catch (SQLException e) { e.printStackTrace(); }finally { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }