第15周作业
一、题目
编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。
二、源程序
Test.java
package pac_7;
import java.sql.*;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("输入用户名密码");
Scanner sc=new Scanner(System.in);
System.out.println("输入用户名:");
String username=sc.nextLine();
System.out.println("输入密码:");
String password=sc.nextLine();
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root","");
ps=con.prepareStatement("select * from t_login where username=? and password=?");
ps.setString(1, username);
ps.setString(2, password);
rs=ps.executeQuery();
if(rs.next()){
System.out.println("登录成功");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs!=null){
rs.close();
}
ps.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
三、运行结果
一、题目
在上一题基础上,当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示(要求使用DB.java完成登录和获取t_user表中数据的操作),最后再对t_user表进行一条记录的添加操作。
二、源程序
DB.java
package pac_8;
import java.sql.*;
public class DB {
private Connection con;
private PreparedStatement pre;
private ResultSet rs;
private static DB db;
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private DB(){
try {
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
} catch (SQLException e) {
e.printStackTrace();
}
}
public static DB getInstance(){
if(db==null){
db=new DB();
}
return db;
}
public ResultSet executeSelect(String sql,Object[] args){
try {
pre=con.prepareStatement(sql);
if(args.length!=0){
for(int i=0;i<args.length;i++){
pre.setObject(i+1, args[i]);
}
}
rs=pre.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public int executeModify(String sql,Object[] args){
int n=0;
try {
pre=con.prepareStatement(sql);
if(args.length!=0){
for(int i=0;i<args.length;i++){
pre.setObject(i+1, args[i]);
}
}
n=pre.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return n;
}
public void close(){
try {
if(rs!=null){
rs.close();
}
pre.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Test.java
package pac_8;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ResultSet rs=null;
DB d=DB.getInstance();
Scanner sc=new Scanner(System.in);
System.out.println("输入用户名:");
String username=sc.nextLine();
System.out.println("输入密码:");
String password=sc.nextLine();
rs=d.executeSelect("select * from t_login where username=? and password=?", new Object[]{username,password});
try {
if(rs.next()){
System.out.println("登陆成功");
rs=d.executeSelect("select * from t_user", new Object[]{});
char sex = ' ';
while(rs.next()){
if(rs.getInt(3)==0){
sex = '男';
}else if(rs.getInt(3)==1){
sex = '女';
}
System.out.println("name:"+rs.getString(2)+" sex:"+sex+" birthday:"+rs.getString(4));
}
System.out.print("对t_user表进行添加一条记录:\n");
String name;
String birthday;
name=sc.nextLine();
sex=sc.nextLine().charAt(0);
birthday=sc.nextLine();
int ssex = -1;
if(sex=='男'){
ssex=0;
}else if(sex=='女'){
ssex=1;
}
if(d.executeModify("insert into t_user (name,sex,birthday)values(?,?,?)", new Object[]{name,ssex,birthday})>0){
System.out.print("添加成功\n");
}
}else{
System.out.print("登录失败\n");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
d.close();
}
}
}
三、运行结果