काम नहीं कर रहा हूं मैं सीख रहा हूं कि जावा के साथ एसक्यूएल का उपयोग कैसे करें। मैंने जेडीबीसी ड्राइवर को सफलतापूर्वक स्थापित किया है और मैं डेटाबेस से रिकॉर्ड्स पढ़ने और स्क्रीन पर प्रिंट करने में सक्षम हूं।जावा में एक्सीक्यूट अपडेट एसक्यूएल कथन
मेरी समस्या तब होती है जब कोई अद्यतन या सम्मिलित कथन करने का प्रयास किया जाता है, जहां कुछ भी नहीं होता है।
विधि जहां समस्या रहता
public static void updateSchools(ArrayList<String> newSchool)
{
try
{
openDatabase();
stmt = c.createStatement();
int numberOfRows = stmt.executeUpdate("UPDATE schools SET address='abc' WHERE abbreviation='2';");
System.out.println(numberOfRows);
closeDatabase();
}
catch (Exception e)
{
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
}
समर्थन कार्यों
public static void openDatabase()
{
c = null;
stmt = null;
try
{
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Badminton", "postgres", "postgrespass");
c.setAutoCommit(false);
}
catch (Exception e)
{
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Database opened successfully");
}
public static void closeDatabase()
{
try
{
stmt.close();
c.close();
}
catch (Exception e)
{
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Database closed successfully");
}
यहाँ मेरी बहुत ही सरल डेटाबेस की एक छवि है:
यहाँ मेरी कोड है कंसोल में परिणाम एफ है ollowing, हालांकि किसी डेटाबेस में परिवर्तन किया गया:
डाटाबेस सफलतापूर्वक
खोलाडाटाबेस पहले से सफलतापूर्वक
धन्यवाद बंद कर दिया!
";" का उपयोग न करें यह एसक्यूएल तोड़ सकता है; क्या आपको कोई अपवाद मिल रहा है? – karelss
माबे आपको अपने डीबी को रीफ्रेश करने की आवश्यकता है, या 'c.setAutoCommit (True); ' – Sami
पोस्टर्रेस से @ karelss [दस्तावेज़ीकरण] (https://www.postgresql.org/docs/6.4/static/install12418.htm) : 'उन एसक्यूएल अर्धविरामों को शामिल करने की आदत में जाओ –