首先將PostgreSQL的JAR檔匯入到Eclipse內:

接著在Eclipse內撰寫:
- import java.sql.*;
- public class MainProcess {
- public static void main(String[] args)
- {
- Connection tempConnection = null;
- Statement tempStatement = null;
- ResultSet tempResultSet = null;
- String tempAction = "";
- String tempSentence = "";
- switch( tempAction )
- {
- case "Select":
- tempSentence = "SELECT * FROM Schemas.Tables WHERE account = 'Kimdick' AND password = '123'";
- break;
- case "Insert":
- tempSentence = "INSERT INTO Schemas.Tables ( account, password ) VALUES ( 'Kimdick', '123' )";
- break;
- case "Update":
- tempSentence = "Update Schemas.Tables SET password = '123', email = 'Kimdick@mail' WHERE account = 'Kimdick'";
- break;
- case "Delete":
- tempSentence = "DELETE FROM Schemas.Tables WHERE account = 'Kimdick'";
- break;
- }
- try
- {
- Class.forName( "org.postgresql.Driver" );
- tempConnection = DriverManager.getConnection( "jdbc:postgresql://127.0.0.1:5432/datastream", "postgres", "1234" );
- tempStatement = tempConnection.createStatement();
- tempStatement.executeQuery( tempSentence );
- if( tempResultSet != null )
- {
- tempResultSet.close();
- tempResultSet = null;
- }
- if( tempStatement != null )
- {
- tempStatement.close();
- tempStatement = null;
- }
- if( tempConnection != null )
- {
- tempConnection.close();
- tempConnection = null;
- }
- }
- catch( Exception e )
- {
- e.printStackTrace();
- }
- }
- }
- DriverManager.getConnection( "jdbc:postgresql://127.0.0.1:5432/datastream", "postgres", "1234" );
這裡是DriverManager.getConnection( "jdbc:postgresql://資料庫IP:Port/Database", 帳號, 密碼 );
沒有留言:
張貼留言