सबसे पहले शीर्षक के नाम के लिए खेद है, लेकिन मुझे नहीं पता कि अंग्रेजी कैसे एक और है क्योंकि अंग्रेजी मेरी मूल भाषा नहीं है।विभिन्न फ़ाइलों का उपयोग कर जेडीबीसी कनेक्शन
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class PgConnect {
public void connect() {
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:postgresql://pgserver:5432/db", "test","test123");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("Connection working");
} else {
System.out.println("Failed to make connection!");
}
}
}
और क्या मैं क्या करने की जरूरत नीचे कोड में PgConnect
से विधि में शामिल है:
मैं एक डेटाबेस से कनेक्ट करने के लिए निम्न विधि है। असल में मुझे इसकी आवश्यकता है क्योंकि मेरे पास डेटाबेस के कई प्रकार के एसक्यूएल कॉल हैं, और इसे इस तरह से बदलना आसान होगा, क्योंकि क्रेडेंशियल/होस्ट केवल एक फाइल पर होंगे।
मेरा मानना है कि मैं कहाँ टिप्पणी
// i want to change this, for using the method on the first file.
कृपया मुझे ठीक कर लें मैं गलत हूँ है परिवर्तन होना चाहिए।
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ReturnResults {
public static void main(String[] args) {
Connection connection = null;
try {
// i want to change this, for using the method on the first file.
connection = DriverManager.getConnection("jdbc:postgresql://pgserver:5432/db", "test","test123");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
String result = null;
String selectString = "select * from mwp.servers where env='TEST' order by server";
//result ="iServer\tLabel\n";
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(selectString);
while (rs.next()) {
String iEnv = rs.getString("env");
String iServer = rs.getString("iserver");
String iLabel = rs.getString("label");
String iTitle = rs.getString("title");
String iLogin = rs.getString("login");
result=iEnv+"\t"+ iServer+"\t"+iLabel+"\t"+iTitle+"\t"+iLogin;
System.out.println(result);
}
stmt.close();
connection.close();
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
} else {
System.out.println("Failed to make connection!");
}
}
}
मुझे पता है कि पर्ल पर ऐसा कैसे करें, लेकिन मेरे पास जावा में कोई अभ्यास नहीं है। क्योंकि आप Connection
नहीं void
वापस जाने के लिए की जरूरत है
पर यह कॉल करने का कोई कारण नहीं है। यह समाधान इरादे के रूप में काम करता था। मैंने आपके द्वारा दिए गए सटीक परेशानी को किया और अब मैं सक्षम हूं आगे बढ़ने के लिए धन्यवाद। –