Skip to content

Instantly share code, notes, and snippets.

@madankumarpc
Created October 17, 2012 11:10
Show Gist options
  • Save madankumarpc/3904990 to your computer and use it in GitHub Desktop.
Save madankumarpc/3904990 to your computer and use it in GitHub Desktop.
Jdbc examples
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