JDBC ile MySQL bağlantısı

package net.yusufsahin.pk1;

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


public class MainProgram {
public static void main(String[] args) {

                   try {
Class.forName(“com.mysql.jdbc.Driver”);
Connection con = DriverManager.getConnection(“jdbc:mysql://localhost/sakila”, “yourUserName”, “yourPassword”);
PreparedStatement ps = con.prepareStatement(“select customer_id,first_name,last_name from customer”);
ResultSet rs = ps.executeQuery();

                                    while(rs.next()){

                                   System.out.println(rs.getInt(“customer_id”)
+“-“+rs.getString(“first_name”)
+” “+rs.getString(“last_name”));
}

                   } catch (Exception {
e.printStackTrace();
}
}
}

Aşağıdaki gibi bir çıktı elde etmelisiniz.


Source: rss