
/*
* Author: qcq
* Date:2015/7/20
* E-mail:qinchuanqing918@163.com
* */
import java.io.BufferedReader
import java.io.File
import java.io.FileReader
import java.io.FileWriter
import java.io.IOException
import java.util.HashMap
import java.util.Map
import java.util.Scanner
import java.util.Set
public class Query {
class Person {
public String Id/*should be sure this is unique*/
public String name
public int age
public boolean sex/*true for man, false for girl*/
@Override
public String toString() {
return Id + " " + name + " " + age + " " + (sex? "M" : "F")
}
}
private Map<String, Person>people
private String fileName
public Query(String fileName){
people = new HashMap<String, Person>()
this.fileName = fileName
if(!initial()){
System.out.println("The initial procedure failed may be because wrong"
+ "store file path, please checkit, and rerun this program.")
}
}
private boolean initial(){
boolean flag = false
try {
File file = new File(fileName)
if (!file.exists()){
System.out.println("The file not exist, we will create it for you")
file.createNewFile()
}
BufferedReader reader = new BufferedReader(new FileReader(fileName))
String line = reader.readLine()
while(null != line &&!line.equals("")){
String[] splitLine = line.split(" +")
if (splitLine.length <4){
System.out.println("The information not enough")
return flag
}
Person temp = new Person()
temp.Id = splitLine[0]
temp.name = splitLine[1]
temp.age = Integer.valueOf(splitLine[2])
temp.sex = splitLine[3].equals("M")? true:false
people.put(temp.Id, temp)
line = reader.readLine()
}
flag = true
} catch (IOException e) {
e.printStackTrace()
}
return flag
}
public boolean writeInFile(){
boolean flag = false
try {
FileWriter writer = new FileWriter(fileName)
Set<String>key = people.keySet()
for (String keyIndex: key){
writer.write(people.get(keyIndex).toString() + "\n")
}
} catch (IOException e) {
e.printStackTrace()
}
return flag
}
/**/
public boolean add(){
return false
}
public boolean delete(){
return false
}
public boolean query(){
return false
}
public boolean update(){
return false
}
public static void main(String[] args){
Query data = new Query("temp.txt")/*here should define the file name*/
Scanner in = new Scanner(System.in)
System.out.println("Here you can choice which option:\n"
+ "quit: for quit"
+ "a: for add"
+ "d: for delete"
+ "u: for update"
+ "q: for query")
String option = in.next()
label:
while(null != option &&!option.equals("")){
switch (option){
case "quit" : break label
case "a" :
/*here add the function*/
data.add()
break
case "d" :
/*here add the function*/
data.delete()
break
case "u":
/*here add the function*/
data.update()
break
case "q" :
/*here add the function*/
data.query()
break
default:
break label
}
}
if (null != in){
in.close()
}
}
}
首先得确定你的数据库连接是通过什么形式连接的,hibernate还是原生态的jdbc 还是spring;如果是只有hibernate,那么你得通过加载配置文件得到sessionFactory,然后得到session
如果spring,那么同样也需要注入sessionfactory到你的dao
如果是jdbc方式,那么你就按照原生态jdbc写法
总之,你构造DAO时,得有数据源。这样才能 *** 纵数据库
字段说明:用户的id 假设是 uid
要修改的内容是 content
sql语句:
update 表名 set content='修改后的内容' where uid='你传过来的用户id'
但这样的话,表的设计肯定是有问题的。
一般一个用户可以对应多个内容,所以这样的话就会修改该用户的多个内容,应该还需要一个内容的唯一 id,一般会有一个 id 字段,主键自增。
这样修改的时候的 where 条件应该至少有两个:id 和 uid
id 是为了保证只修改到一条内容,且是指定的内容;
uid 是为了保证该内容是同一个创作的(当然前端直接传 uid 是不安全的,应该通过 session 和 token 后端再转成用户的 uid)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)