Created
October 17, 2012 11:10
-
-
Save madankumarpc/3904990 to your computer and use it in GitHub Desktop.
Jdbc examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test.connection; | |
import java.sql.*; | |
public class GetData { | |
public static void main( String args[]) { | |
String connectionURL = "jdbc:mysql://localhost:3306/test?user=root&password=root"; | |
// Change the connection string according to your db, ip, username and password | |
Connection con=null; | |
try { | |
// Load the Driver class. | |
Class.forName("com.mysql.jdbc.Driver"); | |
// If you are using any other database then load the right driver here. | |
//Create the connection using the static getConnection method | |
con = DriverManager.getConnection (connectionURL); | |
//Create a Statement class to execute the SQL statement | |
Statement stmt = con.createStatement(); | |
//Execute the SQL statement and get the results in a Resultset | |
ResultSet rs = stmt.executeQuery("select deptno,dname from dept"); | |
// Iterate through the ResultSet, displaying two values | |
// for each row using the getString method | |
while (rs.next()) | |
System.out.println("Name= " + rs.getString("dname") + " Dno= " + rs.getString("deptno")); | |
} | |
catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
} | |
finally { | |
// Close the connection | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment