0
0

More than 1 year has passed since last update.

JAVA×SQL①

Last updated at Posted at 2023-08-28

課題

問題:JAVAでSELECT文を実行せよ(IDが3以下に限る)
使用ソフト:XAMPP、eclipse

package DBA;

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

public class DBA1 {
	public static void main(String[] args) throws SQLException {
		 String url="jdbc:mysql://localhost/JDBC";
		String user ="root";
		String password ="";
		Connection con = DriverManager.getConnection(url,user,password);
		PreparedStatement pstmt =con.prepareStatement
				("SELECT id,name,age FROM woker WHERE id<4");
		ResultSet rs =pstmt.executeQuery();
		while(rs.next()){
			int id =rs.getInt("id");
			String name =rs.getString("name");
			int age=rs.getInt("age");
			System.out.println("ID:"+ id +" 名前:"+ name +" 年齢:"+ age );
		}
		pstmt.close();
		con.close();
	}
	
	}

結果

ID:1 名前:山田太郎 年齢:22
ID:2 名前:花田次郎 年齢:23
ID:3 名前:川田三郎 年齢:22

本日の学び:一回SQLでSELECT文を実行するとわかりやすい

0
0
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
0
0