खोज…


वाक्य - विन्यास

  • DB_URL = "jdbc: DBMS: // DB_HOST: DB_PORT / DB_NAME"

  • DBMS: डेटा बेस ड्राइवर मैनेजर, यह कोई भी DBMS (mysql, oracle, postgresql, sqlite, ...) हो सकता है, mysql की छूट: "com.mysql.jdbc.Dadver"

  • DB_HOST: आपका डेटाबेस बेस होस्ट, आपके डेटाबेस का IP पता छूट: 10.6.0.1, डिफ़ॉल्ट लोकलहोस्ट या 127.0.0.1 है

  • DB_PORT: डेटाबेस पोर्ट, हर DBMS में एक डिफॉल्ट पोर्ट है mysql = 3306, postegesqed = 5432

  • DB_NAME: आपके डेटाबेस का नाम

  • कनेक्ट करने के लिए आपको क्लास ऑब्जेक्ट का संदर्भ प्राप्त करना चाहिए,

  • Class.forName (ड्राइवर);

  • और डेटाबेस से कनेक्ट करने के लिए, आपको एक कनेक्शन बनाने की आवश्यकता है

  • java.sql.Connection con = DriverManager.getConnection (DB_URL, DB_USER_NAME, DB_PASSWORD);

  • DB_USER_NAME: आपके डेटाबेस का उपयोगकर्ता नाम

  • DB_PASSWORD: आपके डेटाबेस का पासवर्ड

परिचय (SQL)

जावा 6 के बाद से, जावा में SQL- आधारित डेटाबेस तक पहुंचने का अनुशंसित तरीका JDBC (जावा डेटाबेस कनेक्टिविटी) एपीआई के माध्यम से है।

यह एपीआई दो पैकेजेज में आता है: java.sql और javax.sql

JDBC Connections और Drivers संदर्भ में डेटाबेस इंटरैक्शन को परिभाषित करता है।

एक Driver डेटाबेस के साथ बातचीत करता है, और कनेक्शन खोलने और प्रबंधित करने के लिए एक सरलीकृत इंटरफ़ेस प्रदान करता है। अधिकांश डेटाबेस सर्वर किस्मों (PostgreSQL, MySQl, आदि) के पास अपने स्वयं के Drivers , जो उस सर्वर के लिए सेटअप, फाड़ और अनुवाद को संभालते हैं। Drivers को आमतौर पर सीधे एक्सेस नहीं किया जाता है; इसके बजाय, DriverManager ऑब्जेक्ट द्वारा प्रदान किए गए इंटरफ़ेस का उपयोग इसके बजाय किया जाता है।

DriverManager ऑब्जेक्ट अनिवार्य रूप से JDBC का मूल है। यह Connections बनाने के लिए एक (अधिकतर) डेटाबेस-एग्नॉस्टिक इंटरफ़ेस प्रदान करता है। JDBC API के पुराने संस्करणों के लिए, डेटाबेस-विशिष्ट Drivers को उस डेटाबेस प्रकार से कनेक्शन बनाने से पहले DeviceManager को लोड करना पड़ता था।

एक Connection है, जैसा कि नाम का तात्पर्य है, डेटाबेस के लिए एक खुले कनेक्शन का प्रतिनिधित्व। Connections डेटाबेस-अज्ञेयवादी हैं, और DriverManager द्वारा बनाए और प्रदान किए जाते हैं। वे सामान्य क्वेरी प्रकारों के साथ-साथ एक कच्चे SQL इंटरफ़ेस के लिए कई 'शॉर्टकट' तरीके प्रदान करते हैं।

कनेक्शन का उपयोग करना (और कथन)

एक बार जब हम Connection प्राप्त कर लेते हैं, तो हम अधिकतर इसका उपयोग Statement ऑब्जेक्ट बनाने के लिए करेंगे। Statements एकल SQL लेनदेन का प्रतिनिधित्व करते हैं; उनका उपयोग किसी क्वेरी को निष्पादित करने और परिणाम (यदि कोई हो) प्राप्त करने के लिए किया जाता है। आइए कुछ उदाहरण देखें:

public void useConnection() throws SQLException{


    Connection conn = getConnection();

    //We can use our Connection to create Statements
    Statement state = conn.getStatement();

    //Statements are most useful for static, "one-off" queries
    
    String query = "SELECT * FROM mainTable";
    boolean sucess = state.execute(query);
    
    //The execute method does exactly that; it executes the provided SQL statement, and returns true if the execution provided results (i.e. was a SELECT) and false otherwise.

    ResultSet results = state.getResultSet();
    
    //The ResultSet object represents the results, if any, of an SQL statement.
    //In this case, the ResultSet contains the return value from our query statement.
    //A later example will examine ResultSets in more detail.

    ResultSet newResults = state.executeQuery(query)
    
    //The executeQuery method is a 'shortcut' method. It combines the execute and getResultSet methods into a single step.
    //Note that the provided SQL query must be able to return results; typically, it is a single static SELECT statement.
    //There are a number of similar 'shortcut' methods provided by the Statement interface, including executeUpdate and executeBatch

    //Statements, while useful, are not always the best choice. 
    
    String newQuery = "SELECT * FROM mainTable WHERE id=?";
    PreparedStatement prepStatement = conn.prepareStatement(newQuery);

    //PreparedStatements are the prefed alternative for variable statements, especially ones that are going to be executed multiple times

    for(int id:this.ids){

        prepStatement.setInt(1,id);
        //PreparedStatements allow you to set bind variables with a wide variety of set methods.
        //The first argument to any of the various set methods is the index of the bind variable you want to set. Note that this starts from 1, not 0. 

        ResultSet tempResults = prepStatement.executeQuery()
        //Just like Statements, PreparedStatements have a couple of shortcut methods. 
        //Unlike Statements, PreparedStatements do not not take a query string as an argument to any of their execute methods.
        //The statement that is executed is always the one passed to the Connector.prepareStatement call that created the PreparedStatement
    }

}

Java.sql.DriverManager का उपयोग करके संबंध बनाना

java.sql.DriverManager का उपयोग कर कनेक्ट करने के लिए आपको अपने डेटाबेस से कनेक्ट करने के लिए एक JDBC यूआरएल की आवश्यकता है। JDBC यूआरएल डेटाबेस विशिष्ट हैं, लेकिन वे सभी फॉर्म हैं

jdbc:<subprotocol>:<subname>

जहाँ <subprotocol> ड्राइवर या डेटाबेस की पहचान करता है (उदाहरण के लिए postgresql , mysql , firebirdsql , आदि), और <subname> सबप्रोकोल-विशिष्ट है।

आपको अपने डेटाबेस और JDBC ड्राइवर के दस्तावेज़ को विशिष्ट url उपप्रोकोल और अपने ड्राइवर के लिए प्रारूप की जाँच करने की आवश्यकता है।

Url jdbc:somedb://localhost/foobar : डेटाबेस से संबंध बनाने के लिए एक सरल उदाहरण:

try (Connection connection = DriverManager.getConnection(
        "jdbc:somedb://localhost/foobar", "anna", "supersecretpassword")) {
    // do something with connection
}

हम यहां एक कोशिश के साथ संसाधनों का उपयोग करते हैं इसलिए कनेक्शन स्वचालित रूप से बंद हो जाता है जब हम इसके साथ होते हैं, भले ही अपवाद होते हैं।

4.0

Java 6 (JDBC 4.0) और इससे पहले के, try-with-resource उपलब्ध नहीं हैं। उन संस्करणों में आपको किसी कनेक्शन को स्पष्ट रूप से बंद करने के लिए finally -ब्लॉक का उपयोग करने की आवश्यकता होती है:

Connection connection = DriverManager.getConnection(
        "jdbc:somedb://localhost/foobar", "anna", "supersecretpassword");
try {
    // do something with connection
} finally {
    // explicitly close connection
    connection.close();
}
4.0

JDBC 4.0 (जावा 6) ने स्वचालित चालक लोडिंग की अवधारणा पेश की। यदि आप जावा 5 या पूर्व या पुराने JDBC ड्राइवर का उपयोग करते हैं जो JDBC 4 समर्थन को लागू नहीं करता है, तो आपको ड्राइवर को स्पष्ट रूप से लोड करने की आवश्यकता होगी:

Class.forName("org.example.somedb.jdbc.Driver");

इस लाइन को आपके प्रोग्राम में एक बार (कम से कम) होने की जरूरत है, इससे पहले कि कोई भी कनेक्शन हो।

जावा 6 में भी और JDBC 4.0 के साथ उच्चतर यह ड्राइवर को स्पष्ट रूप से लोड करने के लिए आवश्यक हो सकता है: उदाहरण के लिए वेब एप्लिकेशन में जब ड्राइवर कंटेनर में लोड नहीं होता है, लेकिन वेब एप्लिकेशन के हिस्से के रूप में।

वैकल्पिक रूप से आप कनेक्ट करने के लिए एक Properties ऑब्जेक्ट भी प्रदान कर सकते हैं:

Properties props = new Properties();
props.setProperty("user", "anna");
props.setProperty("password", "supersecretpassword");
// other, database specific, properties
try (Connection connection = DriverManager.getConnection(
        "jdbc:somedb://localhost/foobar", props)) {
    // do something with connection
}

या गुणों के बिना भी, उदाहरण के लिए यदि डेटाबेस को उपयोगकर्ता नाम और पासवर्ड की आवश्यकता नहीं है:

try (Connection connection = DriverManager.getConnection(
        "jdbc:somedb://localhost/foobar")) {
    // do something with connection
}

MySQL से कनेक्शन बनाना

MySQL से कनेक्ट करने के लिए आपको MySQL कनेक्टर / J ड्राइवर का उपयोग करना होगा। आप इसे http://dev.mysql.com/downloads/connector/j/ से डाउनलोड कर सकते हैं या मावेन का उपयोग कर सकते हैं:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>

MySQL के लिए मूल JDBC URL है:

jdbc:mysql://<hostname>[:<port>]/<database>[?<propertyName>=<propertyValue>[&<propertyName>=<propertyValue>]...]

कहाँ पे:

चाभी विवरण उदाहरण
<hostname> MySQL सर्वर का होस्ट नाम localhost
<port> MySQL सर्वर का पोर्ट (वैकल्पिक, डिफ़ॉल्ट: 3306) 3306
<database> डेटाबेस का नाम foobar
<propertyName> एक कनेक्शन संपत्ति का नाम useCompression
<propertyValue> एक कनेक्शन संपत्ति का मूल्य true

समर्थित URL ऊपर दिखाए गए से अधिक जटिल है, लेकिन यह अधिकांश 'सामान्य' आवश्यकताओं के लिए पर्याप्त है।

उपयोग कनेक्ट करने के लिए:

try (Connection connection = DriverManager.getConnection(
        "jdbc:mysql://localhost/foobardb", "peter", "nicepassword")) {
    // do something with connection
}
4.0

पुराने Java / JDBC संस्करणों के लिए:

4.0
// Load the MySQL Connector/J driver
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(
        "jdbc:mysql://localhost/foobardb", "peter", "nicepassword");
try {
    // do something with connection
} finally {
  // explicitly close connection
  connection.close();
}

UCanAccess के साथ Microsoft Access डेटाबेस से कनेक्शन

UCanAccess एक शुद्ध जावा JDBC ड्राइवर है जो हमें ODBC का उपयोग किए बिना एक्सेस डेटाबेस से पढ़ने और लिखने की अनुमति देता है। यह इन कार्यों को करने के लिए दो अन्य पैकेज, Jackcess और HSQLDB का उपयोग करता है।

एक बार जब यह स्थापित किया गया है *, हम .accdb में डेटा और .mdb फ़ाइलें इस तरह कोड का उपयोग कर के साथ काम कर सकते हैं:

import java.sql.*;

Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:/__tmp/test/zzz.accdb");
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT [LastName] FROM [Clients]");
while (rs.next()) {
    System.out.println(rs.getString(1));
}

* अधिक जानकारी के लिए निम्नलिखित प्रश्न देखें:

ODBC के बिना जावा से एक्सेस डेटाबेस में हेरफेर

Oracle JDBC कनेक्शन

चालक:

( नोट: चालक मावेन सेंट्रल में शामिल नहीं है!)

चालक वर्ग आरंभीकरण:

    Class.forName("oracle.jdbc.driver.OracleDriver");

कनेक्शन URL

पुराने प्रारूप, SID के साथ

"jdbc:oracle:thin:@<hostname>:<port>:<SID>"

सेवा नाम के साथ नया प्रारूप

"jdbc:oracle:thin:@//<hostname>:<port>/<servicename>"

प्रवेश की तरह tnnames

"jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCPS)(HOST=<hostname>)(PORT=<port>))"
    +"(CONNECT_DATA=(SERVICE_NAME=<servicename>)))"

विफलता के लिए आरएसी क्लस्टर कनेक्शन स्ट्रिंग

"jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=OFF)(FAILOVER=ON)"
    +"(ADDRESS=(PROTOCOL=TCP)(HOST=<hostname1>)(PORT=<port1>))"
    +"(ADDRESS=(PROTOCOL=TCP)(HOST=<hostname2>)(PORT=<port2>)))"
    +"(CONNECT_DATA=SERVICE_NAME=<servicename>)(SERVER=DEDICATED)))"

उदाहरण

connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "HR", "HRPASS");


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow