Skip to main content

JDBC Connection in Java

JDBC Connection in Java? 

Supporting library download link : http://jdbc.postgresql.org/download/postgresql-8.4-703.jdbc3.jar 



package com.pukhraj.blog;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DatabaseConnection {

public static void main(String[] args){

try {

Class.forName("org.postgresql.Driver");//"com.mysql.jdbc.Driver"
String url = "jdbc:postgresql://localhost:5432/postgres";//+"jdbc:mysql://localhost/test";
Connection conn = DriverManager.getConnection(url,"username","password");
Statement stmtSelect = conn.createStatement();
String query = "SELECT *  FROM table "; //here you write your SQL query
ResultSet rsSelect = stmtSelect.executeQuery(query);
while(rsSelect.next()){
   long temp = rsSelect.getLong(1);
}
 } catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
 
}

}

Comments

Popular posts from this blog

Common Eclipse Shortcuts for Java Developer Open Resource : ctrl+shift+r Quick Outline : ctrl+o Assign to local variable : ctrl+2, L Rename : alt+shift+r  Extract Local Variable : alt+shift+l  Extract Method : alt+shift+m

Copy Content of One file to another file using Java

Copy Content of One file to another file using Java. package com.pukhraj.blog; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileCopy { public static void main(String[] args) throws IOException {   File inFile = new File("FullPath/inputFileName.extension"); File outFile = new File("FullPath/outputFileName.extension");    FileReader in = new FileReader(inFile);    FileWriter out = new FileWriter(outFile);    int i;    while ((i = in.read()) != -1)      out.write(i);    in.close();    out.close();   } }