Subversion Repository Public Repository

FP_REPORT_SYNC_DB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.nru.test;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;

public class Utilities {
	Connection con;
	FileReader reader;
	String mysql_driver_string="";
	String mysql_connection_string="";
	String mysql_username="";
	String mysql_password="";
	String query ="";

	public Utilities() {
		this.con = getConnection();
	}

	public Connection getConnection(){
		try {
			reader = new FileReader("config.properties");
			Properties properties=new Properties();
			properties.load(reader);
			String mysql_driver_string=properties.getProperty("mysql_driver_string");
			String mysql_connection_string=properties.getProperty("mysql_connection_string_local");
			String mysql_username=properties.getProperty("mysql_username_local");
			String mysql_password=properties.getProperty("mysql_password_local");
			//System.out.println("mysql_driver_string: "+mysql_driver_string+"\nmysql_connection_string: "+mysql_connection_string+"\nmysql_username: "+mysql_username+"\nmysql_password: "+mysql_password);
			this.mysql_driver_string = mysql_driver_string;
			this.mysql_connection_string = mysql_connection_string;
			this.mysql_username = mysql_username;
			this.mysql_password = mysql_password;

			Class.forName(mysql_driver_string).newInstance();
			Connection con = DriverManager.getConnection(mysql_connection_string, mysql_username, mysql_password);
			System.out.println("-- connected --");
			this.con = con;
		} catch (Exception e) {e.printStackTrace();}
		return con;
	}
	
	public Connection getConnection_remote(){
		try {
			reader = new FileReader("config.properties");
			Properties properties=new Properties();
			properties.load(reader);
			String mysql_driver_string=properties.getProperty("mysql_driver_string");
			String mysql_connection_string=properties.getProperty("mysql_connection_string_remote");
			String mysql_username=properties.getProperty("mysql_username_remote");
			String mysql_password=properties.getProperty("mysql_password_remote");
			//System.out.println("mysql_driver_string: "+mysql_driver_string+"\nmysql_connection_string: "+mysql_connection_string+"\nmysql_username: "+mysql_username+"\nmysql_password: "+mysql_password);
			this.mysql_driver_string = mysql_driver_string;
			this.mysql_connection_string = mysql_connection_string;
			this.mysql_username = mysql_username;
			this.mysql_password = mysql_password;

			Class.forName(mysql_driver_string).newInstance();
			Connection con = DriverManager.getConnection(mysql_connection_string, mysql_username, mysql_password);
			System.out.println("-- connected --");
			this.con = con;
		} catch (Exception e) {e.printStackTrace();}
		return con;
	}
	
	public void closeConnection(Connection con) {
		try {
			con.close();
		} catch (Exception e) {e.printStackTrace();}
	}

	public Vector selectQuery(String query, int params, Connection con){
		Vector result = new Vector();
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery(query);
			while (rs.next()) {
				Vector temp = new Vector();
				for (int i = 1; i <= params; i++) {
					String str = (String) rs.getString(i);
					temp.add(str);
				}
				result.add(temp);
			}
		} catch (Exception e) {e.printStackTrace();}
		return result;
	}
	
	public String getField(String query, Connection con){
		String result = "";
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery(query);
			while (rs.next()) {
				result = (String) rs.getString(1);
			}
		} catch (Exception e) {e.printStackTrace();}
		return result;
	}

	public void insertQuery(String query, Connection con){
		try {
			Statement st = con.createStatement();
			int ins = st.executeUpdate(query);
			//System.out.println("  >>  ins: "+ins);
		} catch (Exception e) {e.printStackTrace();}
	}
}

Commits for FP_REPORT_SYNC_DB/src/com/nru/test/Utilities.java

Diff revisions: vs.
Revision Author Commited Message
1 lingaraj picture lingaraj Sat 24 Nov, 2018 12:32:58 +0000