Java基础培训--第七节

file io流 ——文件的复制转移

这里讲的是将一个文件夹里的文件经过复制转移到另外一个文件夹,其实现代码如下:
int bs=0;
File allFiles =new File(“C:\Users\asus\Desktop\作业\text1”);
String []str=allFiles.list();
for(String name:str)
System.out.println(name);
for(int i=0;i<str.length;i++) {
String name=str[i];
File oldFile =new File(“C:\Users\asus\Desktop\作业\text1\”+name);
File newFile=new File(“C:\Users\asus\Desktop\作业\text2\”+name);
FileInputStream fis=new FileInputStream(oldFile);
FileOutputStream fos=new FileOutputStream(newFile);
while((bs=fis.read())!=-1) {
fos.write(bs);
}
fis.close();
fos.close();
}

项目:音乐管理系统的制作

首先是分包,分为bean 实体类,dao 数据库增删改查,service 业务层,util 工具层。
bean里存放着User.java,其内代码为:
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return “User{” +
“id=” + id +
“, username=’” + username + ‘’’ +
“, password=’” + password + ‘’’ +
‘}’;
}
dao中的Userdao里的代码如下:
public static boolean login (String username,String password)throws SQLException{
Connection connection= DBUtil.getConection();
String sql =“select * from ub_user where username=? and password=?”;
PreparedStatement preparedStatement= connection.prepareStatement(sql);
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
ResultSet resultSet=preparedStatement.executeQuery();
if(resultSet.next()){
return true;
}else {
return false;
}
}
public static boolean zhuce (String usermane,String password)throws SQLException{
Connection connection =DBUtil.getConection();
String sql=“insert into ub_user (username,password)values (?,?)”;
PreparedStatement statement =connection.prepareStatement(sql);
statement.setString(1,usermane);
statement.setString(2,password);
int i =statement.executeUpdate();
if (i!=0){
return true;
} else
return false;
}
service里的MusicService代码如下:
public static void main(String[] args) throws Exception{
System.out.println("----------请输入用户名:----------");
Scanner input =new Scanner(System.in);
String username =input.next();
System.out.println("----------请输入密码:----------");
String password =input.next();
System.out.println(username+password);
Userbao.login(username,password);
boolean flag=Userbao.login(username,password);
if (flag)
{
System.out.println("----------登陆成功----------");
System.out.println("----------欢迎来到音乐管理系统----------");

}
else
{
    System.out.println("----------登录失败----------");
    System.out.println("----------用户注册----------");
    System.out.println("请输入注册名:");
    String zcm =input.next();
    System.out.println("请输入注册密码:");
    String mm =input.next();
    System.out.println("请确认密码:");
    String mm1 =input.next();
    if(mm.equals(mm1))
    {
        Userbao.zhuce(zcm,mm);
    }else
    {
        System.out.println("密码有误,请重新注册");
    }
}

}
util的DBUtil的代码如下:
public static Connection getConection(){
Connection connection=null ;
try {
Class.forName(“com.mysql.jdbc.Driver”);
connection= DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/2?useSSL=true&characterEncoding=utf-8&user=root&password=123456”
);}catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static void closeAll(ResultSet rs, PreparedStatement preparedStatement, Connection connection)
{
if (rs!=null)
{
try {
rs.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if(preparedStatement!=null)
{
try {
preparedStatement.close();
} catch (SQLException e){
e.printStackTrace();
}
}
if(connection!=null)
{
try {
connection.close();
} catch (SQLException e){
e.printStackTrace();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值