Java Topics
HubJava Collections FrameworkCollections Practice ProgramsComparable vs ComparatorGenericsIteratorArrayListLinkedListStackVectorHashMapHashtableLinkedHashMapTreeMapArrayDequeDeque Double-Ended QueuePriorityQueueHashSetLinkedHashSetTreeSetAdapter PatternBuilder PatternFactory PatternMVC PatternObserver PatternSingleton PatternDynamic ProgrammingGraphHashingHeapLinked List DSAQueue DSARecursionSearching AlgorithmsSorting AlgorithmsStack DSATime ComplexityTreesException Handling Best PracticesCustom ExceptionException Hierarchyfinally BlockException Handlingthrow aur throwstry-catchBuffered StreamsByte StreamsCharacter StreamsDeserializationFile ClassJava NIO New I/OSerializationArrays in JavaArray OperationsArray Practice ProgramsArray Interview QuestionsJagged ArrayMulti-Dimensional ArraysOne Dimensional Arraybreak aur continuedo-while Loopfor Loopif-else StatementsNested Loopsswitch-case Statementwhile LoopJava Compilation ProcessJava ki FeaturesPehla Java ProgramHistory of JavaJava EditionsJava Program StructureJDK, JRE aur JVMJava Kya Hai?Immutable StringsString Class in JavaString Interview QuestionsString MethodsString Practice ProgramsStringBufferStringBuilderComments in JavaData Types in JavaIdentifiers in JavaInput & Output in JavaJava KeywordsOperators in JavaType Casting in JavaVariables in JavaJava Coding Interview QuestionsCollections Interview QuestionsCore Java Interview QuestionsJDBC Interview QuestionsMultithreading Interview QuestionsOOPs Interview QuestionsSpring Framework Interview QuestionsJava 8 Date/Time APIDefault aur Static Methods in InterfaceFunctional InterfaceLambda ExpressionMethod ReferenceOptional ClassStream APIBatch ProcessingCallableStatementJDBC ArchitectureJDBC — Java Database ConnectivityJDBC Practice ProjectsMySQL ConnectionPreparedStatementResultSetStatement InterfaceTransaction ManagementCreating ThreadsDaemon ThreadExecutor FrameworkInter-Thread CommunicationMultithreading Practice ProgramsRunnable InterfaceSynchronizationThread ClassMultithreading IntroductionThread Life CycleThread PriorityJava Cheat SheetImportant Formulas & Key ConceptsImportant Java ProgramsJava Quick RevisionAbstract ClassAbstractionAnonymous ClassClass and ObjectConstructorEncapsulationInheritanceInner ClassInterfaceMethod OverloadingMethod OverridingObject ClassObject CloningObject-Oriented Programming OOPPolymorphismstatic Keywordsuper Keywordthis KeywordWrapper ClassesArray Practice ProgramsBasic Java Practice ProgramsCollection Practice ProgramsJDBC Practice ProgramsMultithreading Practice ProgramsOOPs Practice ProgramsPlacement Coding QuestionsString Practice ProgramsBanking SystemChat ApplicationE-Commerce Backend — Spring Boot REST APIEmployee Management SystemLibrary Management SystemSpring Boot Fullstack ProjectStudent Management SystemCookies in ServletExpression Language ELGenericServletHttpServletJavaServer Pages JSPJSP TagsMVC ArchitectureHttpServletRequest & HttpServletResponseServlet IntroductionServlet Life CycleSession TrackingDependency InjectionSpring BeansSpring CoreSpring FrameworkAPI GatewaySpring Cloud Config ServerDocker DeploymentEureka Server — Service DiscoverySpring Boot CRUD ApplicationException Handling in Spring BootJWT AuthenticationREST API in Spring BootSpring SecuritySpring BootSpring Data JPAValidation in Spring Boot
MySQL Connection
Last Updated : 26 May, 2026
title: MySQL Connection in JDBC
JDBC25 words2 headingsExamples included
title: MySQL Connection in JDBC description: MySQL se Java program connect karna
Properties File se Connection (Best Practice)
java exampleWoHoTech
// db.properties file
// db.url=jdbc:mysql://localhost:3306/school
// db.user=root
// db.password=password
class DBConfig {
private static Properties props = new Properties();
static {
try (InputStream is = DBConfig.class.getResourceAsStream("/db.properties")) {
props.load(is);
} catch (IOException e) { e.printStackTrace(); }
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(
props.getProperty("db.url"),
props.getProperty("db.user"),
props.getProperty("db.password")
);
}
}CRUD Operations — Complete Example
java exampleWoHoTech
class StudentDAO {
// INSERT
void insert(String name, int age, double marks) throws SQLException {
String sql = "INSERT INTO students (name, age, marks) VALUES (?, ?, ?)";
try (Connection conn = DBConfig.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, name);
ps.setInt(2, age);
ps.setDouble(3, marks);
int rows = ps.executeUpdate();
System.out.println(rows + " row inserted");
}
}
// SELECT ALL
List<String> getAll() throws SQLException {
List<String> result = new ArrayList<>();
String sql = "SELECT * FROM students";
try (Connection conn = DBConfig.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next())
result.add(rs.getInt("id") + " - " + rs.getString("name") + " - " + rs.getDouble("marks"));
}
return result;
}
// UPDATE
void update(int id, double newMarks) throws SQLException {
String sql = "UPDATE students SET marks = ? WHERE id = ?";
try (Connection conn = DBConfig.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setDouble(1, newMarks);
ps.setInt(2, id);
ps.executeUpdate();
}
}
// DELETE
void delete(int id) throws SQLException {
String sql = "DELETE FROM students WHERE id = ?";
try (Connection conn = DBConfig.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, id);
ps.executeUpdate();
}
}
}Exam Focus
Revise definitions, diagrams, examples, and short-answer points for MySQL Connection.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java topic.
Search Terms
java, java programming, core java, java master course, java notes, master, course, jdbc
Related Java Topics
Continue learning this concept
JDBCBatch Processingtitle: Batch Processing in JDBCJDBCCallableStatementtitle: CallableStatement in JDBCJDBCJDBC Architecturetitle: JDBC ArchitectureJDBCJDBC — Java Database Connectivitytitle: JDBC IntroductionJDBCJDBC Practice Projectstitle: JDBC Mini ProjectsJDBCPreparedStatementtitle: PreparedStatement in JDBC