Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/default-filters.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/default-filters.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-theme.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-theme.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-styles.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-styles.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-request.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-request.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/block-supports/duotone.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/block-supports/duotone.php on line 1

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768
{"id":35,"date":"2011-04-03T20:23:48","date_gmt":"2011-04-03T14:23:48","guid":{"rendered":"http:\/\/ujjalruet.wordpress.com\/?p=35"},"modified":"2011-04-03T20:23:48","modified_gmt":"2011-04-03T14:23:48","slug":"connecting-with-mysql-in-java","status":"publish","type":"post","link":"https:\/\/blog.ujjal.net\/?p=35","title":{"rendered":"Connecting with MySQL in JAVA"},"content":{"rendered":"

Sometimes we need to use database(MySQL).So,we must connect our interpreter to the existing MySQl.For this we need JDBC driver to install it.In netbeans IDE jdbc driver is built in.But to make this working, we must have a mysql connector,which is a jar file in our system. <\/p>\n

We must download a mysql-connector-java-5.0.8-bin.jar and keep it to
\nC:Program FilesJavajdk1.6.0_01jrelibext<\/p>\n

In java programming, before coding we must established a connection between interpreter and MySQL.<\/p>\n

at first we must import some built in packages.they are:
\n[code]
\nimport java.sql.DriverManager;
\nimport java.sql.Statement;
\nimport java.sql.ResultSet;
\nimport java.sql.SQLException;
\nimport java.sql.Connection;<\/p>\n

\/\/now finally in our main job i.e establishing the connection<\/p>\n

Connection m_Connection = null;
\nStatement m_Statement = null;
\nResultSet m_ResultSet = null;<\/p>\n

String m_Driver ="com.mysql.jdbc.Driver";
\nString m_Url = "jdbc:mysql:\/\/localhost:3306\/databasename";<\/p>\n

\/\/Loading driver
\ntry {
\nClass.forName(m_Driver);
\n}
\ncatch (ClassNotFoundException ex) {
\nex.printStackTrace();
\n}<\/p>\n

String query = "";
\ntry {<\/p>\n

\/\/Create connection object
\nm_Connection = DriverManager.getConnection(m_Url, user, password);<\/p>\n

\/\/Create Statement object
\nm_Statement = m_Connection.createStatement();
\nquery = "SELECT * FROM ";
\nquery+=tablename;<\/p>\n

\/\/Execute the query
\nm_ResultSet = m_Statement.executeQuery(query);
\nString s,t="";<\/p>\n

\/\/Loop through the results
\nwhile (m_ResultSet.next()) {<\/p>\n

s=m_ResultSet.getString(1); \/\/here 1 means value of first coloumn<\/p>\n

}\/\/end for while loop<\/p>\n

}
\ncatch (SQLException ex) {
\nex.printStackTrace();
\nSystem.out.println(query);<\/p>\n

}
\ncatch (Exception e) {
\nSystem.err.println("Error: " + e.getMessage());<\/p>\n

}<\/p>\n

finally {<\/p>\n

try {
\nif (m_ResultSet != null)
\nm_ResultSet.close();
\nif (m_Statement != null)
\nm_Statement.close();
\nif (m_Connection != null)
\nm_Connection.close();
\n}
\ncatch (SQLException ex) {
\nex.printStackTrace();
\n}
\n}
\n[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"

Sometimes we need to use database(MySQL).So,we must connect our interpreter to the existing MySQl.For this we need JDBC driver to install it.In netbeans IDE jdbc driver is built in.But to make this working, we must have a mysql connector,which is a jar file in our system. We must download a mysql-connector-java-5.0.8-bin.jar and keep it to … <\/p>\n