STEPS :
1.Download the postgre JDBC driver
2.While executing the program first GRANT ALL PRIVILEGES ON TABLE ttt TO aaa;
3.set the class path for the .jar driver
export CLASSPATH=/home/exam/Desktop/postgresql.jar:.
4.execute the program
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
/**
* @author www.javaworkspace.com
*
*/
public class ConnectPostgre {
public static void main(String[] args) {
Connection connection = null;
ResultSet resultSet = null;
Statement statement = null;
try {
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/jobportal", "postgres","kiran");
statement = connection.createStatement();
Scanner sc=new Scanner(System.in);
System.out.println("enter data for new company:cid;name;branch;email;website;address;username;password");
cid=sc.nextInt();
name=sc.next();
branch=sc.next();
email=sc.next();
website=sc.next();
address=sc.next();
username=sc.next();
password=sc.next();
//String sql = "INSERT INTO patient " +
// "VALUES (4, 'Zara', 'Ali','nama','2013-06-10' ,'1993-6-5')";
String sql = "INSERT INTO company " +
"VALUES ("+cid+", '"+name+"', '"+branch+"','"+email+"','"+website+"' ,'"+address+"' ,'"+username+"' ,'"+password+"')";
statement.executeUpdate(sql);
resultSet = statement.executeQuery("SELECT * FROM company");
while (resultSet.next())
{
System.out.print("Company NAME:"
+ resultSet.getString("name"));
System.out.print(" Company Branch:"
+ resultSet.getString("branch"));
System.out.print(" Company Email:"
+ resultSet.getString("email"));
System.out.print(" Companyt Website:"
+ resultSet.getString("website"));
System.out.println(" Company Address:"
+ resultSet.getString("address"));
System.out.println(" Company Username:"
+ resultSet.getString("username"));
System.out.println(" Company Password:"
+ resultSet.getString("password"));
}
//System.out.println("enter data for new patient:id;fname;mname;lname;apptime;dob")
//int id ;String fname,mname,lname;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
OLD OUTPUT
exam@exam-OptiPlex-390:~/Desktop$ export CLASSPATH=/home/exam/Desktop/postgresql.jar:.
exam@exam-OptiPlex-390:~/Desktop$ javac ConnectPostgre.java
exam@exam-OptiPlex-390:~/Desktop$ java ConnectPostgre
patient FNAME:ramu
patient MNAME:papu
patient LNAME:lal
patient time:2013-10-10
patient DOB:1993-10-10
patient FNAME:asd
patient MNAME:ssss
patient LNAME:2er
patient time:null
patient DOB:1993-06-12
patient FNAME:qqqq
patient MNAME:cu
patient LNAME:okedemes
patient time:2013-01-04
patient DOB:2013-06-08
exam@exam-OptiPlex-390:~/Desktop$
NEW OUTPUT
exam@exam-OptiPlex-390:~/Desktop$ export CLASSPATH=/home/exam/Desktop/postgresql.jar:.
exam@exam-OptiPlex-390:~/Desktop$ javac ConnectPostgre.java exam@exam-OptiPlex-390:~/Desktop$ java ConnectPostgreenter data for new patient:id;fname;mname;lname;apptime;
dob
6
new
tttt
yyyy
2013-2-5
1995-2-1
patient FNAME:ramu
patient MNAME:papu
patient LNAME:lal
patient time:2013-10-10
patient DOB:1993-10-10
patient FNAME:asd
patient MNAME:ssss
patient LNAME:2er
patient time:null
patient DOB:1993-06-12
patient FNAME:qqqq
patient MNAME:cu
patient LNAME:okedemes
patient time:2013-01-04
patient DOB:2013-06-08
patient FNAME:Zara
patient MNAME:Ali
patient LNAME:nama
patient time:2013-06-10
patient DOB:1993-06-05
patient FNAME:w
patient MNAME:aa
patient LNAME:aaa
patient time:2013-05-09
patient DOB:1993-05-04
patient FNAME:new
patient MNAME:tttt
patient LNAME:yyyy
patient time:2013-02-05
patient DOB:1995-02-01
exam@exam-OptiPlex-390:~/Desktop$
*/
No comments:
Post a Comment