master
Siryuanshao 6 years ago
parent a36e6c345a
commit c75bf5ecb3

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$USER_HOME$/javapackage/mysql-connector-java-8.0.13/mysql-connector-java-8.0.13.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

Binary file not shown.

@ -0,0 +1,7 @@
Book 6
BookID NUM
BookName STRING
Author STRING
Price NUM
Introduce STRING
Type NUM

@ -0,0 +1,27 @@
package sqlconnect;
import java.util.ArrayList;
public class Add {
public static void insert(String table,ArrayList<String> value){
if(Database.Maps.containsKey(table)==false){
System.err.println("没有找到该表");
return;
}
ArrayList<String> Word=Readclass.getWord(table,value);
int sz=Word.size();
StringBuffer sqlQue=new StringBuffer();
sqlQue.append("insert into "+table+" values (");
for(int i=0;i<sz;i++){
sqlQue.append(Word.get(i)+(i==sz-1?")":","));
}
try {
System.err.println(sqlQue.toString());
Database.state.execute(sqlQue.toString());
System.out.println("插入成功");
}catch (Exception e){
System.err.println("插入失败");
e.printStackTrace();
}
}
}

@ -0,0 +1,8 @@
package sqlconnect;
class Constant {
static final String mysql="com.mysql.cj.jdbc.Driver";
static final String url="jdbc:mysql://45.76.158.31:3306/tushuxiaoshou";
static final String user="root";
static final String passwd="yuanshao";
}

@ -0,0 +1,34 @@
package sqlconnect;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.HashMap;
public class Database {
static HashMap<String, Item> Maps = new HashMap();
static Connection conn;
static Statement state;
public static void init() throws Exception {
Class.forName(Constant.mysql);
Readclass.init();
conn = DriverManager.getConnection(Constant.url,Constant.user,Constant.passwd);
state = conn.createStatement();
}
public static void close() {
if(state != null) {
try{
state.close();
}catch (Exception e) {
e.printStackTrace();
}
}
if(conn != null) {
try{
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
}

@ -0,0 +1,19 @@
package sqlconnect;
public class Del {
public static void delete(String table,String attribute,String value){
if(Database.Maps.containsKey(table)==false){
System.err.println("没有找到该表");
return;
}
attribute=SQLfilter.filter(attribute);
value=SQLfilter.filter(value);
String sqlQue="delete from "+table+" where "+attribute+"="+value;
try{
Database.state.execute(sqlQue);
}catch (Exception e){
System.err.println("删除失败");
e.printStackTrace();
}
}
}

@ -0,0 +1,23 @@
package sqlconnect;
import java.util.ArrayList;
public class Item {
public ArrayList<String> key=new ArrayList<String>();
public ArrayList<String> type=new ArrayList<String>();
public int size=0;
public void print(ArrayList<ArrayList<String>> arrayLists){
for(int i=0;i<size;i++){
System.out.printf("%-20s ",key.get(i)+"("+type.get(i)+")");
}
System.out.println();
for(int i=0;i<arrayLists.size();i++){
ArrayList<String> tmp=arrayLists.get(i);
for(int j=0;j<tmp.size();j++){
System.out.printf("%-20s ",tmp.get(j));
}
System.out.println();
}
System.out.println();
}
}

@ -0,0 +1,67 @@
package sqlconnect;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static String path = "/Users/yanshao/PycharmProjects/untitled2/Books/";
public static String suffixname(String name){
if(name.indexOf('.')==-1) return null;
else return name.substring(name.lastIndexOf(".") + 1);
}
public static String getIntroduce(String path){
path += ".txt";
StringBuffer ret=new StringBuffer();
try{
File file=new File(path);
Scanner fin=new Scanner(file);
while(fin.hasNext()){
ret.append(fin.next()+"\n");
}
}catch (Exception e){
e.printStackTrace();
}
return ret.toString();
}
public static void main(String[] args){
try{
Database.init();
File file=new File(path);
File files[]=file.listFiles();
if(files==null) return;
ArrayList<File> Type=new ArrayList<File>();
for(int i=0;i<files.length;i++){
if(files[i].isDirectory())
Type.add(files[i]);
}
for(int i=0;i<Type.size();i++){
File tp[]=Type.get(i).listFiles();
for(File f:tp){
String name=f.getName();
if(suffixname(name)==null){
//好了,在这里的就是每一本书的信息了
Scanner fin=new Scanner(f);
String BookID=fin.next();
String BookName=fin.next();
String Price=fin.next();
String Author=fin.next();
String Introduce=getIntroduce(f.getAbsolutePath());
String typed=String.valueOf(i);
ArrayList<String> data=new ArrayList<String>();
data.add(BookID);
data.add(BookName);
data.add(Author);
data.add(Price);
data.add(Introduce);
data.add(typed);
Add.insert("Book",data);
}
}
}
Database.close();
}catch (Exception e){
e.printStackTrace();
}
}
}

@ -0,0 +1,58 @@
package sqlconnect;
import java.sql.ResultSet;
import java.util.ArrayList;
public class Que {
private static ArrayList<String> getAttribute(Item item,ResultSet rs){
ArrayList<String> arrayList=new ArrayList<>();
try{
for(int i=0;i<item.size;i++){
String key=item.key.get(i);
String type=item.type.get(i);
if(type=="INT") arrayList.add(String.valueOf(rs.getInt(key)));
else arrayList.add(String.valueOf(rs.getString(key)));
}
}catch (Exception e){
e.printStackTrace();
}
return arrayList;
}
public static ArrayList<ArrayList<String>> QueryAll(String table){
ArrayList<ArrayList<String>> data=new ArrayList<>();
if(Database.Maps.containsKey(table)==false){
System.err.println("没有找到该表");
return data;
}
Item item=Database.Maps.get(table);
String sqlQue="select * from "+table;
try{
ResultSet rs=Database.state.executeQuery(sqlQue);
while(rs.next()){
data.add(getAttribute(item,rs));
}
}catch (Exception e){
e.printStackTrace();
}
return data;
}
public static ArrayList<ArrayList<String>> QueryBook(String name){
ArrayList<ArrayList<String>> arrayLists=new ArrayList<>();
name=SQLfilter.filter(name);
String sqlQue="select book.bookno,bookname,author from book " +
"join readinghistory rh on book.bookno=rh.bookno " +
"join student on rh.sno=student.sno where sname='"+name+"'";
System.out.println(sqlQue);
Item item=Database.Maps.get("book");
try{
ResultSet rs=Database.state.executeQuery(sqlQue);
while(rs.next()){
arrayLists.add(getAttribute(item,rs));
}
}catch (Exception e){
System.err.println("查询失败");
e.printStackTrace();
}
return arrayLists;
}
}

@ -0,0 +1,46 @@
package sqlconnect;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
public class Readclass {
public static void init(){
try{
Scanner fin=new Scanner(new File("src/sqlconnect/variable"));
while(fin.hasNext()){
String className=fin.next();
int count=fin.nextInt();
Item item=new Item();
item.size=count;
for(int i=0;i<count;i++){
String name=fin.next();
String type=fin.next();
item.key.add(name);
item.type.add(type);
}
Database.Maps.put(className,item);
}
}catch (Exception e){
System.err.println("配置文件错误");
e.printStackTrace();
}
}
public static ArrayList<String> getWord(String table,ArrayList<String> value){
Item item=Database.Maps.get(table);
ArrayList<String> arrayList=new ArrayList<String>();
for(int i=0;i<item.size;i++){
String type=item.type.get(i);
String val=value.get(i);
if(val.equals("NULL")) {
arrayList.add("NULL");
}else if(type.equals("NUM")){
arrayList.add(SQLfilter.filter(val));
}else if(type.equals("STRING")){
arrayList.add("'"+SQLfilter.filter(val)+"'");
}
}
return arrayList;
}
}

@ -0,0 +1,14 @@
package sqlconnect;
public class SQLfilter {
private static String delete="`~!@#$^&*()=|{}':;',\\[].<>/?~@#¥……&*()——|{}";
public static String filter(String sqlQue){
// StringBuffer sqlfiler=new StringBuffer();
// for(int i=0;i<sqlQue.length();i++){
// char sign=sqlQue.charAt(i);
// if(delete.indexOf(sign)==-1) sqlfiler.append(sign);
// }
// return sqlfiler.toString();
return sqlQue;
}
}

@ -0,0 +1,23 @@
package sqlconnect;
public class Upd {
public static void update(String table,String attribute1,String value1,
String attribute2,String value2){
if(Database.Maps.containsKey(table)==false){
System.err.println("没有找到该表");
return;
}
attribute1=SQLfilter.filter(attribute1);
value1=SQLfilter.filter(value1);
attribute2=SQLfilter.filter(attribute2);
value2=SQLfilter.filter(value2);
String sqlQue="update "+table+" set "+attribute2+"="+value2+" where "+
attribute1+"="+value1;
try{
Database.state.execute(sqlQue);
}catch (Exception e){
System.err.println("删除失败");
e.printStackTrace();
}
}
}

@ -0,0 +1,7 @@
Book 6
BookID NUM
BookName STRING
Author STRING
Price NUM
Introduce STRING
Type NUM
Loading…
Cancel
Save