LoginSignup
1
2

More than 5 years have passed since last update.

JAVA DB接続方法

Posted at

JAVAでDBに接続するコードのメモ

完全に理解していないのでここから理解を深めていく。

public static void main(String[] args) {
        //DB接続
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            Class.forName("org.postgresql.Driver");
            conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/DB名",
                    "ユーザー",
                    "パスワード"
                    );
            //SQL文を定義する
            String sql = "INSERT INTO テーブル名(●, ●, ●) values(?,?,?)";
            ps = conn.prepareStatement(sql);
            conn.close();
            System.out.println("DBに接続しました。");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("失敗しました。");
        }
        launch();
    }
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2