Advertisement
Guest User

Untitled

a guest
Aug 8th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. Context context = new InitialContext();
  2. DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/myDB");
  3.  
  4. MysqlDataSource dataSource = new MysqlDataSource();
  5. dataSource.setUser("scott");
  6. dataSource.setPassword("tiger");
  7. dataSource.setServerName("myDBHost.example.org");
  8.  
  9. Connection conn = dataSource.getConnection();
  10. Statement stmt = conn.createStatement();
  11. ResultSet rs = stmt.executeQuery("SELECT ID FROM USERS");
  12. ...
  13. rs.close();
  14. stmt.close();
  15. conn.close();
  16.  
  17. .
  18.  
  19. CREATE DATABASE javabase DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
  20.  
  21. CREATE USER 'java'@'localhost' IDENTIFIED BY 'password';
  22. GRANT ALL ON javabase.* TO 'java'@'localhost' IDENTIFIED BY 'password';
  23.  
  24. hostname
  25.  
  26. main()
  27.  
  28. String url = "jdbc:mysql://localhost:3306/javabase";
  29. String username = "java";
  30. String password = "password";
  31.  
  32. System.out.println("Connecting database...");
  33.  
  34. try (Connection connection = DriverManager.getConnection(url, username, password)) {
  35. System.out.println("Database connected!");
  36. } catch (SQLException e) {
  37. throw new IllegalStateException("Cannot connect the database!", e);
  38. }
  39.  
  40. System.out.println("Loading driver...");
  41.  
  42. try {
  43. Class.forName("com.mysql.jdbc.Driver");
  44. System.out.println("Driver loaded!");
  45. } catch (ClassNotFoundException e) {
  46. throw new IllegalStateException("Cannot find the driver in the classpath!", e);
  47. }
  48.  
  49. String url = "jdbc:mysql://127.0.0.1:3306/yourdatabase";
  50. String user = "username";
  51. String password = "password";
  52.  
  53. // Load the Connector/J driver
  54. Class.forName("com.mysql.jdbc.Driver").newInstance();
  55. // Establish connection to MySQL
  56. Connection conn = DriverManager.getConnection(url, user, password);
  57.  
  58. Class.forName("com.mysql.jdbc.Driver").newInstance();
  59. Connection conn = DriverManager.getConnection
  60. ("jdbc:mysql://localhost:3306/foo", "root", "password");
  61.  
  62. Statement stmt = conn.createStatement();
  63. stmt.execute("SELECT * FROM `FOO.BAR`");
  64. stmt.close();
  65. conn.close();
  66.  
  67. // init database constants
  68. private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
  69. private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/database_name";
  70. private static final String USERNAME = "root";
  71. private static final String PASSWORD = "";
  72. private static final String MAX_POOL = "250"; // set your own limit
  73.  
  74. // init connection object
  75. private Connection connection;
  76. // init properties object
  77. private Properties properties;
  78.  
  79. // create properties
  80. private Properties getProperties() {
  81. if (properties == null) {
  82. properties = new Properties();
  83. properties.setProperty("user", USERNAME);
  84. properties.setProperty("password", PASSWORD);
  85. properties.setProperty("MaxPooledStatements", MAX_POOL);
  86. }
  87. return properties;
  88. }
  89.  
  90. // connect database
  91. public Connection connect() {
  92. if (connection == null) {
  93. try {
  94. Class.forName(DATABASE_DRIVER);
  95. connection = DriverManager.getConnection(DATABASE_URL, getProperties());
  96. } catch (ClassNotFoundException | SQLException e) {
  97. // Java 7+
  98. e.printStackTrace();
  99. }
  100. }
  101. return connection;
  102. }
  103.  
  104. // disconnect database
  105. public void disconnect() {
  106. if (connection != null) {
  107. try {
  108. connection.close();
  109. connection = null;
  110. } catch (SQLException e) {
  111. e.printStackTrace();
  112. }
  113. }
  114. }
  115.  
  116. import java.sql.Connection;
  117. import java.sql.DriverManager;
  118. import java.sql.SQLException;
  119. import java.util.Properties;
  120.  
  121. public class MysqlConnect {
  122. // init database constants
  123. private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
  124. private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/database_name";
  125. private static final String USERNAME = "root";
  126. private static final String PASSWORD = "";
  127. private static final String MAX_POOL = "250";
  128.  
  129. // init connection object
  130. private Connection connection;
  131. // init properties object
  132. private Properties properties;
  133.  
  134. // create properties
  135. private Properties getProperties() {
  136. if (properties == null) {
  137. properties = new Properties();
  138. properties.setProperty("user", USERNAME);
  139. properties.setProperty("password", PASSWORD);
  140. properties.setProperty("MaxPooledStatements", MAX_POOL);
  141. }
  142. return properties;
  143. }
  144.  
  145. // connect database
  146. public Connection connect() {
  147. if (connection == null) {
  148. try {
  149. Class.forName(DATABASE_DRIVER);
  150. connection = DriverManager.getConnection(DATABASE_URL, getProperties());
  151. } catch (ClassNotFoundException | SQLException e) {
  152. e.printStackTrace();
  153. }
  154. }
  155. return connection;
  156. }
  157.  
  158. // disconnect database
  159. public void disconnect() {
  160. if (connection != null) {
  161. try {
  162. connection.close();
  163. connection = null;
  164. } catch (SQLException e) {
  165. e.printStackTrace();
  166. }
  167. }
  168. }
  169. }
  170.  
  171. // !_ note _! this is just init
  172. // it will not create a connection
  173. MysqlConnect mysqlConnect = new MysqlConnect();
  174.  
  175. String sql = "SELECT * FROM `stackoverflow`";
  176. try {
  177. PreparedStatement statement = mysqlConnect.connect().prepareStatement(sql);
  178. ... go on ...
  179. ... go on ...
  180. ... DONE ....
  181. } catch (SQLException e) {
  182. e.printStackTrace();
  183. } finally {
  184. mysqlConnect.disconnect();
  185. }
  186.  
  187. Connection con = DriverManager.getConnection(
  188. "jdbc:myDriver:DatabaseName",
  189. dBuserName,
  190. dBuserPassword);
  191.  
  192. Statement stmt = con.createStatement();
  193. ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table");
  194.  
  195. while (rs.next()) {
  196. int x = rs.getInt("a");
  197. String s = rs.getString("b");
  198. float f = rs.getFloat("c");
  199. }
  200.  
  201. Connection con = DriverManager.getConnection(
  202. "jdbc:myDriver:DatabaseName",
  203. dBuserName,
  204. dBuserPassword);
  205. if (con != null){
  206. //..handle your code there
  207. }
  208.  
  209. try
  210. {
  211. Class.forName("com.mysql.jdbc.Driver");
  212. System.out.println("Driver Loaded");
  213. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testDB","root","");
  214. //Database Name - testDB, Username - "root", Password - ""
  215. System.out.println("Connected...");
  216. }
  217. catch(Exception e)
  218. {
  219. e.printStackTrace();
  220. }
  221.  
  222. try
  223. {
  224. String url = "jdbc:sqlserver://KHILAN:1433;databaseName=testDB;user=Khilan;password=Tuxedo123";
  225. //KHILAN is Host and 1433 is port number
  226. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  227. System.out.println("Driver Loaded");
  228. conn = DriverManager.getConnection(url);
  229. System.out.println("Connected...");
  230. }
  231. catch(Exception e)
  232. {
  233. e.printStackTrace();
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement