当前位置:编程学习 > JAVA >>

java中如何对本文做增加,删除,查找,修改?

例如我有数据
001  小明  
002  小华
003  小红

我想删除小华
或者修改小红
或者查找小明
或者增加
004 小东 --------------------编程问答-------------------- 这是主要的方法,数据库的连接和用户类自己编写吧
public class JdbcDAO {
 private Connection conn = null;
 private PreparedStatement ps = null;
 private ResultSet rs = null;
Scanner sc = new Scanner(System.in);
public boolean insert(User user){
try {
conn=JdbcUtil.getConnection();
String sql = "INSERT INTO t_user VALUES(?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,user.getId());
ps.setString(2,user.getName());

int x = ps.executeUpdate();
if (x>0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
JdbcUtil.close(conn, ps);
}
return false;
}
public boolean update(User user){

try {
conn =JdbcUtil.getConnection();
String sql = "update t_user set name=? where id=?";
ps = conn.prepareStatement(sql);
ps.setString(1,user.getName());

ps.setString(2,user.getId());
int x = ps.executeUpdate();
System.out.println(x);
if (x>0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
JdbcUtil.close(conn, ps);
}
return false;
}

public ArrayList select(){
ArrayList<User> list = new ArrayList<User>();
try {
conn = JdbcUtil.getConnection();
String sql = "select id,name from t_user ";
PreparedStatement ps =conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
User user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
list.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
JdbcUtil.close(conn, ps ,rs);
}

return list;
}
public boolean delete(String id){

try {
conn = JdbcUtil.getConnection();
String sql = "delete from t_user where id=? ";
ps =conn.prepareStatement(sql);
ps.setString(1, id);
int x =ps.executeUpdate();
if (x>0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
JdbcUtil.close(conn, ps );
}
return false;
}
public User modiy(String id){
User user = null;
try {
conn = JdbcUtil.getConnection();
String sql = "select id,name from t_user where id = ? ";
 ps =conn.prepareStatement(sql);
ps.setString(1, id);
 rs = ps.executeQuery();
if(rs.next()){
user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
JdbcUtil.close(conn, ps ,rs);
}
return user;
}
}
--------------------编程问答-------------------- 用FileInputStream读取文本,然后进行字符串处理。。。 --------------------编程问答-------------------- --------------------编程问答-------------------- 很感谢一楼,但是只是操作文本,不能用数据库
二楼说的很有道理,但是怎么实现操作呢,我感觉不是很轻松,能试着写写么? --------------------编程问答-------------------- 代码仅供参考。这段代码只是把数据读出来的。你删掉,修改,增加都需要从新往文件里面写入。这些你就自己想想吧。

package com.test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class Test {

public static void main(String[] args){
List<Person> persons = getPersonsInfo();
for(Person p : persons){
System.out.println("[id:" + p.getId() + "; name:" + p.getName() + "]");
}
}

public static List<Person> getPersonsInfo(){
List<Person> persons = new ArrayList<Person>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("D:\\workspace\\test\\src\\test.txt"));
while(true){
String line = reader.readLine();
if(line == null){
break;
}
String[] strs = line.split(" ");
if(strs.length == 2){
persons.add(new Person(strs[0], strs[1]));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return persons;
}
}

class Person{
private String id;
private String name;

public Person(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}
--------------------编程问答-------------------- 获得配置中的数据(List或Array)
添加、删除、修改
重新写入配置文件 --------------------编程问答-------------------- 给LZ一个删除的代码参考参数是这个用户的ID号
public static boolean deleteUser(int num) {
File file = new File("./info/userInfo.txt");
boolean flag = false;
BufferedReader reader = null;
BufferedWriter writer = null;
String str = null;
List list = null;
try {
list = new ArrayList();
reader = new BufferedReader(new FileReader(file));
while ((str = reader.readLine()) != null) {
String[] strArray = str.split(",");
int id = Integer.parseInt(strArray[0]);
if (id == num) {
continue;
}
list.add(str);
}
writer = new BufferedWriter(new FileWriter(file));
for (int i = 0; i < list.size(); i++) {
String templine = (String) list.get(i);
writer.write(templine);
writer.newLine();
writer.flush();
}
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
--------------------编程问答-------------------- 还是把方法都给你写了,虽然只有20分。

public static void wirteFile(){
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("D:\\workspace\\test\\src\\test.txt"));
for(Person person : persons){
writer.write(person.getId() + " " + person.getName() + "\n");
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(writer != null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public static void addPerson(Person person){
persons.add(person);
wirteFile();
}

public static void updatePerson(Person newPerson, Person oldPerson){
if(persons != null){
for(Person person : persons){
if(person.equals(oldPerson)){
person = newPerson;
}
}
wirteFile();
}
}

public static void deletePerson(Person person){
if(persons != null){
for(int i = 0; i < persons.size(); i++){
Person p = persons.get(i);
if(p.equals(person)){
persons.remove(i);
}
}
wirteFile();
}
}
--------------------编程问答-------------------- person对象做了修改:

class Person{
private String id;
private String name;

public Person(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Person other = (Person) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,