Commit a1bd919a authored by ElaFHNW's avatar ElaFHNW
Browse files

database use

parent 2f47a546
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class DatabaseUse {
private static String file = "C:\\Users\\ElaFHNW\\eclipse-workspace\\se_as21\\src\\data1.csv";
private static ArrayList<List<String>> data = new ArrayList<>();
private static Connection con = null;
public DatabaseUse() {
con = connect();
readFromFile();
// System.out.println("read "+ data.size() + " entries");
List<String> header = data.remove(0);
createTable(header,con);
// insert(data);
// printCount();
// close();
}
private void readFromFile() {
File fi = null;
try {
fi = new File(file);
Scanner scan = new Scanner(fi);
while (scan.hasNext()) {
String line = scan.nextLine();
String [] myarr = line.split(";");
List<String> lineArray = new ArrayList<String> ();
for (String st : myarr) {
lineArray.add(st);
}
//List<String> lineArray = Arrays.asList(myarr);
data.add(lineArray);
System.out.println(lineArray);
}
scan.close();
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
private void createTable(List<String> list, Connection con) {
try {
Statement statement= con.createStatement();
statement.executeUpdate("drop table if exists table1");
String sql = "create table table1 (\n";
sql += "ID INTEGER PRIMARY KEY AUTOINCREMENT,\n";
String lastOne = list.remove(list.size()-1);
for (String h : list) {
sql += h + " INTEGER,\n";
}
sql += lastOne+ " INTEGER\n";
sql += ")";
System.out.println(sql);
statement.executeUpdate(sql);
}
catch (SQLException se) {
System.out.println(se.getMessage());
}
}
private void insert(ArrayList<List<String>> data2) {
// insecure sql
// String sql = "INSERT INTO table1 (Header1, Header2, Header3) VALUES (";
// INSERT INTO table1 (Header1, Header2, Header3) VALUES (33,44,55);
PreparedStatement stmt = null;
// INSERT INTO table1 (Header1, Header2, Header3) VALUES (?,?,?);
// for (List<String> st : data2)
stmt = con.prepareStatement("INSERT INTO table1 (Header1, Header2, Header3) VALUES (?,?,?)");
for
// variable binding
stmt.setString(1, st.get(0));
stmt.setString(2, );
stmt.setString(3, );
stmt.
}
public void printCount() {
}
public void close() {
if (con != null) {
try {
con.close();
}
catch (SQLException se){
System.out.println(se.getMessage());
}
}
}
public static Connection connect() {
String serverInfo = "jdbc:sqlite:setestdb.db";
System.out.println("Opening connection to " + serverInfo + "\n");
try {
con = DriverManager.getConnection(serverInfo);
}
catch (SQLException se){
System.out.println(se.getMessage());
}
return con;
}
public static void main(String[] args) {
DatabaseUse db2 = new DatabaseUse();
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment