Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class ConnectDB {
  4.  
  5. private static Connection connect;
  6. private static ConnectDB instance;
  7.  
  8. private ConnectDB()
  9. {
  10.  
  11. try {
  12.  
  13. Class.forName("com.mysql.jdbc.Driver");
  14. //connect DB
  15. connect = DriverManager.getConnection("jdbc:mysql://ip/database","root","password");
  16.  
  17. }
  18.  
  19. catch(SQLException e)
  20. {
  21. System.err.println(e.getMessage());
  22.  
  23. }
  24.  
  25. catch(ClassNotFoundException e)
  26. {
  27.  
  28. System.err.println(e.getMessage());
  29.  
  30. }
  31. }
  32.  
  33. public static ConnectDB getInstance()
  34. {
  35.  
  36. if(instance == null) {
  37.  
  38. instance = new ConnectDB();
  39.  
  40. }
  41.  
  42. return instance;
  43.  
  44. }
  45.  
  46. }
  47.  
  48. public class NameClass {
  49.  
  50.  
  51.  
  52.  
  53. public void getInfoDatabase()
  54. {
  55.  
  56. Connection cnn = ConnectDB.getConnection();
  57. PreparedStatement ps;
  58. ResultSet rs;
  59.  
  60. try {
  61.  
  62. ps = cnn.prepareStatement("select * from tables");
  63.  
  64. rs = ps.executeQuery();
  65.  
  66. while(rs.next())
  67. {
  68.  
  69. String tables = rs.getString("table1");
  70. System.out.println(tables);
  71. }
  72.  
  73. public class DBConnection{
  74.  
  75. private static DBConnection instance;
  76. private String url="jdbc:oracle:thin:@192.168.10.32:1521:orcl";
  77. private String login="kit";
  78. private String pass="1234";
  79.  
  80. private DBConnection(){
  81.  
  82. try {
  83. Class.forName("oracle.jdbc.driver.OracleDriver");
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. }
  87. }
  88.  
  89. public static Connection getConnection() throws SQLException {
  90. if (instance == null) {
  91. instance = new DBConnection();
  92. System.out.println(" Connection - - - - - - - - New DBConnection created");
  93. }
  94. try {
  95. return DriverManager.getConnection(instance.url, instance.login,instance.pass);
  96. } catch (SQLException e) {
  97. throw e;
  98. }
  99. }
  100.  
  101. public static void close(Connection connection)
  102. {
  103. try {
  104. if (connection != null) {
  105. connection.close();
  106. connection=null;
  107. }
  108. } catch (SQLException e) {
  109. e.printStackTrace();
  110. }
  111. }
  112.  
  113. }
  114.  
  115. public class ContactDAO {
  116.  
  117.  
  118. public List<Contact> findAll() {
  119. List<Contact> list = new ArrayList<Contact>();
  120. Connection c = null;
  121. String sql = "SELECT * FROM KIT.CONTACT";
  122. try {
  123. c = DBConnection.getConnection();
  124. Statement s = c.createStatement();
  125. ResultSet rs = s.executeQuery(sql);
  126. while (rs.next()) {
  127. list.add(processRow(rs));
  128. }
  129. } catch (SQLException e) {
  130. e.printStackTrace();
  131. throw new RuntimeException(e);
  132. } finally {
  133. DBConnection.close(c);
  134. }
  135. return list;
  136. }
  137.  
  138. public List<Contact> findByCity(String city) {
  139. List<Contact> list = new ArrayList<Contact>();
  140. Connection c = null;
  141. String sql = "SELECT * FROM KIT.CONTACT as e " + "WHERE UPPER(city) LIKE ? ";
  142. try {
  143. c = DBConnection.getConnection();
  144. PreparedStatement ps = c.prepareStatement(sql);
  145. ps.setString(1, "%" + city.toUpperCase() + "%");
  146. ResultSet rs = ps.executeQuery();
  147. while (rs.next()) {
  148. list.add(processRow(rs));
  149. }
  150. } catch (SQLException e) {
  151. e.printStackTrace();
  152. throw new RuntimeException(e);
  153. } finally {
  154. DBConnection.close(c);
  155. }
  156. return list;
  157. }
  158.  
  159. public Contact findById(int id) {
  160. String sql = "SELECT * FROM KIT.CONTACT WHERE id = ?";
  161. Contact contact = null;
  162. Connection c = null;
  163. try {
  164. c = DBConnection.getConnection();
  165. PreparedStatement ps = c.prepareStatement(sql);
  166. ps.setInt(1, id);
  167. ResultSet rs = ps.executeQuery();
  168. if (rs.next()) {
  169. contact = processRow(rs);
  170. }
  171. } catch (Exception e) {
  172. e.printStackTrace();
  173. throw new RuntimeException(e);
  174. } finally {
  175. DBConnection.close(c);
  176. }
  177. return contact;
  178. }
  179.  
  180. public Contact save(Contact contact) {
  181. return contact.getId() > 0 ? update(contact) : insert(contact);
  182. }
  183.  
  184. public Contact insert(Contact contact) {
  185. Connection c = null;
  186. PreparedStatement ps = null;
  187. try {
  188. c = DBConnection.getConnection();
  189. ps = c.prepareStatement(
  190. "INSERT INTO KIT.CONTACT (country, city, address, photo,fk_user) VALUES (?, ?, ?, ?, ?)",
  191. new String[] { "ID" });
  192.  
  193. ps.setString(1, contact.getCountry());
  194. ps.setString(2, contact.getCity());
  195. ps.setString(3, contact.getAddress());
  196. ps.setString(4, contact.getPhoto());
  197. ps.setInt(5, contact.getFk_user());
  198.  
  199. ps.executeUpdate();
  200.  
  201. ResultSet rs = ps.getGeneratedKeys();
  202. while(rs.next()){
  203. int id = rs.getInt(1);
  204. contact.setId(id);
  205. }
  206.  
  207.  
  208. } catch (Exception e) {
  209. e.printStackTrace();
  210. throw new RuntimeException(e);
  211. } finally {
  212. DBConnection.close(c);
  213. }
  214. return contact;
  215. }
  216.  
  217. public Contact update(Contact contact) {
  218. Connection c = null;
  219. try {
  220. c = DBConnection.getConnection();
  221. PreparedStatement ps = c
  222. .prepareStatement("UPDATE KIT.CONTACT SET country=?, city=?, address=?, photo=? WHERE id=?");
  223. ps.setString(1, contact.getCountry());
  224. ps.setString(2, contact.getCity());
  225. ps.setString(3, contact.getAddress());
  226. ps.setString(4, contact.getPhoto());
  227.  
  228. ps.setInt(5, contact.getId());
  229. ps.executeUpdate();
  230. } catch (SQLException e) {
  231. e.printStackTrace();
  232. System.out.println("contactDAO update exception");
  233. throw new RuntimeException(e);
  234. } finally {
  235. DBConnection.close(c);
  236. }
  237. return contact;
  238. }
  239.  
  240. public boolean remove(int id) {
  241. Connection c = null;
  242. try {
  243. c = DBConnection.getConnection();
  244. PreparedStatement ps = c
  245. .prepareStatement("DELETE FROM KIT.CONTACT WHERE id=?");
  246. ps.setInt(1, id);
  247. int count = ps.executeUpdate();
  248. return count == 1;
  249. } catch (Exception e) {
  250. e.printStackTrace();
  251. throw new RuntimeException(e);
  252. } finally {
  253. DBConnection.close(c);
  254. }
  255. }
  256.  
  257. protected Contact processRow(ResultSet rs) throws SQLException {
  258. Contact contact = new Contact();
  259. contact.setId(rs.getInt("id"));
  260. contact.setCountry(rs.getString("country"));
  261. contact.setCity(rs.getString("city"));
  262. contact.setAddress(rs.getString("address"));
  263. contact.setPhoto(rs.getString("photo"));
  264.  
  265. return contact;
  266.  
  267. }
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement