· 9 years ago · Jun 19, 2017, 06:10 PM
1
2import java.sql.*;
3public class dbmanager {
4 final private String DB_URL = "jdbc:mysql://localhost/newdb";
5 final private String JDBC_DRIVER = "com.mysql.jdbc.Driver";
6 private String sql="", sql2="";
7 final private String USER = "root";
8 final private String PASS = "Meghali01/12";
9 private Connection conn = null;
10 private Statement stmt = null;
11
12 public dbmanager() {
13
14 try{
15 Class.forName(JDBC_DRIVER);
16
17 this.conn = DriverManager.getConnection(DB_URL, USER, PASS);
18
19 this.stmt = conn.createStatement();
20
21
22 this.sql = "CREATE TABLE IF NOT EXISTS newtable " +
23 "(mailid INTEGER not NULL UNIQUE AUTO_INCREMENT, " +
24 " sender VARCHAR(255), " +
25 " subject VARCHAR(255), " +
26 " message VARCHAR(5000), " +
27 " PRIMARY KEY ( mailid ))";
28
29 stmt.execute(sql);
30 System.out.println("Table created successfully with date");
31
32 this.sql = "CREATE TABLE IF NOT EXISTS media"
33 + "(id INT NOT NULL UNIQUE AUTO_INCREMENT,"
34 + "folder VARCHAR(200) NOT NULL,"
35 + "FOREIGN KEY(id) REFERENCES newtable(mailid)"
36 + "ON DELETE CASCADE);";
37 stmt.executeUpdate(sql);
38 System.out.println("Table created successfully for media");
39 if(stmt!=null)
40 stmt.close();
41 if(conn!=null)
42 conn.close();
43
44 }catch(Exception se){
45 System.out.println(se);
46 }
47 }
48
49
50 public void insertDATA(int id, String sender, String subject, String message){
51 try{
52 this.conn = DriverManager.getConnection(this.DB_URL, this.USER, this.PASS);
53 this.stmt = conn.createStatement();
54
55 System.out.println("inserting:"+subject+" "+sender);
56
57 String sql = "INSERT INTO newtable " + " (mailid, sender, subject, message)" + " values('" + id + "', '" +
58 sender + "', '" + subject + "', '" + message + "');";
59
60 stmt.execute(sql);
61
62
63 ResultSet rs=stmt.executeQuery("select last_insert_id() as last_id from newtable");
64 rs.next();
65 String lastid=rs.getString("last_id");
66 System.out.println("hi");
67
68
69
70 int email_id=Integer.parseInt(lastid);
71 System.out.println("emailId:"+String.valueOf(id));
72
73 insertMedia(email_id,"c:\\Users\\HP\\Desktop");
74
75
76 }catch(Exception e){
77 System.out.println(e);
78 }
79 }
80 public void insertMedia(int email_id, String folder){
81 try{
82 this.conn = DriverManager.getConnection(this.DB_URL, this.USER, this.PASS);
83
84 this.stmt = conn.createStatement();
85
86 System.out.println("inserting media:"+folder);
87 String sql="INSERT INTO media"
88 +"(id, folder)"+
89 "values('"+email_id+"','"+folder+"');";
90 stmt.execute(sql);
91 }catch(Exception e){
92 System.out.println(e);
93 }
94
95
96 }
97}