Netbeans de JDBC ile Mysql Bağlantısı

1. Öncelikle Mysql JDBC driver ini indiriyoruz.

Connector-J 3.1.14

2. Bu jar dosyasını projemizin “Libraries” kısmına ekliyoruz.

image

3. Database bağlantısı için class imizda aşağıdaki fonksiyonu oluşturuyoruz.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public Connection connect(String conURL, String user, String pass) throws SQLException {

     Connection connection = null;

     // load jdbc driver
     try {
          Class.forName("com.mysql.jdbc.Driver");
     } catch (ClassNotFoundException ex) {
          Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
     }

     connection = DriverManager.getConnection(conURL, user, pass);

     return connection;

}

4. Bir query çalıştırmak için aşağıdaki kodu kullanıyoruz.

1
2
3
connection = jdbc_connect.connect(connectionURL, connectionUser, connectionPass);
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM deneme");

5. Yukarıdaki koddan oluşan result seti aşağıdaki kod ile ekrana bastırabilir.

1
2
3
4
5
while (rs.next()) {
     System.out.println(rs.getString("id"));
     System.out.println(rs.getString("name"));
     System.out.println(rs.getString("surname"));
}

6. Insert, update ve delete için aşağıdaki kodu kullanıyoruz.

1
statement.executeUpdate(sqlQuery);

7. Hepsini bir araya toplarsak aşağıdaki class oluşur.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package saxparser;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.applet.Main;

/**
 *
 * @author ugur
 */

public class JDBC_Connect {

    public Connection connect(String conURL, String user, String pass) throws SQLException {
        Connection connection = null;

        // load jdbc driver
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        connection = DriverManager.getConnection(conURL, user, pass);
        return connection;
    }

    public static void main(String[] args) throws SQLException {

        String connectionURL = "jdbc:mysql://localhost:3306/ugur?";
        String connectionUser = "root";
        String connectionPass = "pass";
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;

        try {            
            JDBC_Connect jdbc_connect = new JDBC_Connect();

            connection = jdbc_connect.connect(connectionURL, connectionUser, connectionPass);
            statement = connection.createStatement();
            rs = statement.executeQuery("SELECT * FROM deneme");

            while (rs.next()) {
                System.out.println(rs.getString("id"));
                System.out.println(rs.getString("name"));
                System.out.println(rs.getString("surname"));
            }

            String name = "ugur";
            String surname = "donmez";
            String sqlQuery = "INSERT INTO deneme (name,surname) VALUES (\"" + name + "\",\"" + surname + "\")";


            statement.executeUpdate(sqlQuery);

        } catch (SQLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            connection.close();
        }

    }
}

Leave a Reply