Guest User

android_oracle

a guest
May 23rd, 2016
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.sql.*;
  7.  
  8. public class ConnectOracle {
  9.  
  10. public static void main(String[] args) {
  11.  
  12. String driver = "oracle.jdbc.driver.OracleDriver"; //
  13.  
  14. String serverName = "localhost";
  15. String portNumber = "1521";
  16. String db = "XE";
  17. String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":"
  18. + db; // connectOracle is the data
  19. // source name
  20. String user = "system"; // username of oracle database
  21. String pwd = "root"; // password of oracle database
  22. Connection con = null;
  23. ServerSocket serverSocket = null;
  24. Socket socket = null;
  25. DataInputStream dataInputStream = null;
  26. DataOutputStream dataOutputStream = null;
  27.  
  28. try {
  29. Class.forName(driver);// for loading the jdbc driver
  30.  
  31. System.out.println("JDBC Driver loaded");
  32.  
  33. con = DriverManager.getConnection(url, user, pwd);// for
  34. // establishing
  35. // connection
  36. // with database
  37. Statement stmt = con.createStatement();
  38.  
  39. serverSocket = new ServerSocket(8888);
  40. System.out.println("Listening :8888");
  41.  
  42. while (true) {
  43. try {
  44.  
  45. socket = serverSocket.accept();
  46. System.out.println("Connection Created");
  47. dataInputStream = new DataInputStream(
  48. socket.getInputStream());
  49. dataOutputStream = new DataOutputStream(
  50. socket.getOutputStream());
  51. System.out.println("ip: " + socket.getInetAddress());
  52. // System.out.println("message: " +
  53. // dataInputStream.readUTF());
  54.  
  55. ResultSet res=stmt.executeQuery("select * from food_category");
  56. while(res.next()){
  57. System.out.println(res.getString(1));
  58. }
  59.  
  60. } catch (IOException e) {
  61. // TODO Auto-generated catch block
  62. e.printStackTrace();
  63. }
  64.  
  65. if (dataInputStream != null) {
  66. try {
  67. dataInputStream.close();
  68. } catch (IOException e) {
  69. // TODO Auto-generated catch block
  70. e.printStackTrace();
  71. }
  72. }
  73.  
  74. if (dataOutputStream != null) {
  75. try {
  76. dataOutputStream.close();
  77. } catch (IOException e) {
  78. // TODO Auto-generated catch block
  79. e.printStackTrace();
  80. }
  81. }
  82. }
  83. } catch (Exception e) {
  84. // TODO Auto-generated catch block
  85. e.printStackTrace();
  86. }
  87. }
  88. }
Add Comment
Please, Sign In to add comment