{"id":4362,"date":"2021-05-24T04:34:53","date_gmt":"2021-05-24T04:34:53","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=4362"},"modified":"2021-05-24T06:33:50","modified_gmt":"2021-05-24T06:33:50","slug":"insert-dummy-data-using-faker-libraryjava","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/","title":{"rendered":"Insert dummy data using Faker library(Java)"},"content":{"rendered":"<p>Faker library is used to generating the test data. In automation, testing the data generating process is one of the most frustrating processes. For data-driven testing, we need a lot of data that we generate manually or get the file from the internet to fill the fake data. This wastes a lot of time in both terms creating test data. Faker allows us to model this data and create fresh dynamic values for fields every time.<\/p>\n<p>In this code, we are generating the \u201cname\u201d and \u201ccompany name\u201d using the data faker library. Then store this data into the database directly by using the Insert query. This way we can insert bulk data into the <a href=\"https:\/\/studysection.com\/blog\/how-to-generate-laravel-migration-using-existing-database\/\">database<\/a> to test the behavior of the website. <\/p>\n<p><strong>To run this code we need to create a maven project so that we can add faker dependency to import the faker package in the project.<\/strong><\/p>\n<h2>Steps to create a Project:<\/h2>\n<ol>\n<li>Create a new maven project in the Eclipse.<\/li>\n<li>Create a new package and a file.<\/li>\n<li>Add the below-mentioned dependency in the pom file of the project:<br \/>\n<code><em>&lt;!--https:\/\/mvnrepository.com\/artifact\/com.github.javafaker\/javafaker --><br \/>\n&lt;dependency><br \/>\n&lt;groupId>com.github.javafaker&lt;\/groupId><br \/>\n&lt;artifactId>javafaker&lt;\/artifactId><br \/>\n&lt;version>0.16&lt;\/version><br \/>\n&lt;\/dependency><\/em><\/code>\n<\/li>\n<li>Now right click on the project and go to the maven.<\/li>\n<li>Click on the update project.<\/li>\n<li>Paste the below code in the created file and run the project:<br \/>\n<code>package TestingAutomation.testingfaker;<br \/>\nimport java.sql.Connection;<br \/>\nimport java.sql.DriverManager;<br \/>\nimport java.sql.PreparedStatement;<br \/>\nimport java.sql.ResultSet;<br \/>\nimport java.sql.SQLException;<br \/>\nimport java.sql.Statement;<br \/>\nimport com.github.javafaker.Faker;<br \/>\npublic class Testing{<br \/>\n\t\/\/creating faker object<br \/>\n\tstatic Faker faker = new Faker();<br \/>\n\tpublic static String getName() {<br \/>\n\t\t\/\/Generating the first name<br \/>\n\t\tString Name = faker.name().name();<br \/>\n\t\treturn Name;<br \/>\n\t}<br \/>\n\tpublic static String getCompany() {<br \/>\n\t\t\/\/Generating company name<br \/>\n\t\tString Company = faker.company().name();<br \/>\n\t\treturn Company;<br \/>\n\t}<br \/>\n\tpublic static void main(String arg[]) throws SQLException,<br \/>\n\tClassNotFoundException {<br \/>\n\t\t\/\/Intialialization of variables<br \/>\n\t\tString url = \"jdbc:mysql:\/\/127.0.0.1:3306\/testing?user=root\";<br \/>\n\t\t\/\/Loading the required MYSQL JDBC Driver class<br \/>\n\t\tClass.forName(\"com.mysql.cj.jdbc.Driver\");<br \/>\n\t\t\/\/Creating a connection to the database<br \/>\n\t\tConnection conn = DriverManager.getConnection(url);<br \/>\n\t\t\/\/ Insert 10 records in the database<br \/>\n\t\tint count = 0;<br \/>\n\t\twhile (count < 10) {\n\t\t\t\/\/Executing SQL query and fetching the result\n\t\t\tStatement fetchData = conn.createStatement();\n\t\t\t\/\/ Execute the Insert command\n\t\t\tString excuteComm = \"INSERT INTO testing.testing(name,company)VALUES(?,?)\";\n\t\t\tPreparedStatement preparedStmt = conn.prepareStatement(excuteComm, Statement.RETURN_GENERATED_KEYS);\n\t\t\tpreparedStmt.setString(1, getName());\n\t\t\tpreparedStmt.setString(2, getCompany());\n\t\t\t\/\/ Execute the preparedstatement\n\t\t\tpreparedStmt.executeUpdate();\n\t\t\t\/\/ Getting Id of last Inserted records in the database\n\t\t\tResultSet result = preparedStmt.getGeneratedKeys();\n\t\t\tint id = 0;\n\t\t\tif (result.next()) {\n\t\t\t\tid = result.getInt(1);\n\t\t\t}\n\t\t\tSystem.out.println(\"Last Inserted ID:\" + id);\n\t\t\t\/\/Disconnect the database\n\t\t\tfetchData.close();\n\t\t\tcount++;\n\t\t}\n\t}\n}<\/code><\/p>\n<h3>Result:<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/05\/demo.png\" alt=\"Faker library\"\/>\n<\/li>\n<\/ol>\n<p><small>If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level <a href=\"https:\/\/www.studysection.com\/php-web-development-advanced\">PHP Certification Exams<\/a> are offered by StudySection along with other programming certification exams.<\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Faker library is used to generating the test data. In automation, testing the data generating process is one of the<\/p>\n","protected":false},"author":1,"featured_media":4363,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[167,623],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Insert dummy data using Faker library(Java) - SS Blog<\/title>\n<meta name=\"description\" content=\"Faker library is used to generating the test data. In automation, testing data generating process is one of the most frustrating processes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Insert dummy data using Faker library(Java) - SS Blog\" \/>\n<meta property=\"og:description\" content=\"Faker library is used to generating the test data. In automation, testing data generating process is one of the most frustrating processes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-24T04:34:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-24T06:33:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/05\/java.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Insert dummy data using Faker library(Java)\",\"datePublished\":\"2021-05-24T04:34:53+00:00\",\"dateModified\":\"2021-05-24T06:33:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/\"},\"wordCount\":263,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"java\",\"Library\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/\",\"url\":\"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/\",\"name\":\"Insert dummy data using Faker library(Java) - SS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2021-05-24T04:34:53+00:00\",\"dateModified\":\"2021-05-24T06:33:50+00:00\",\"description\":\"Faker library is used to generating the test data. In automation, testing data generating process is one of the most frustrating processes.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Insert dummy data using Faker library(Java)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Insert dummy data using Faker library(Java) - SS Blog","description":"Faker library is used to generating the test data. In automation, testing data generating process is one of the most frustrating processes.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/","og_locale":"en_US","og_type":"article","og_title":"Insert dummy data using Faker library(Java) - SS Blog","og_description":"Faker library is used to generating the test data. In automation, testing data generating process is one of the most frustrating processes.","og_url":"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2021-05-24T04:34:53+00:00","article_modified_time":"2021-05-24T06:33:50+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/05\/java.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Insert dummy data using Faker library(Java)","datePublished":"2021-05-24T04:34:53+00:00","dateModified":"2021-05-24T06:33:50+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/"},"wordCount":263,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["java","Library"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/","url":"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/","name":"Insert dummy data using Faker library(Java) - SS Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2021-05-24T04:34:53+00:00","dateModified":"2021-05-24T06:33:50+00:00","description":"Faker library is used to generating the test data. In automation, testing data generating process is one of the most frustrating processes.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/insert-dummy-data-using-faker-libraryjava\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Insert dummy data using Faker library(Java)"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":1959,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/4362"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=4362"}],"version-history":[{"count":3,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/4362\/revisions"}],"predecessor-version":[{"id":4367,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/4362\/revisions\/4367"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/4363"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=4362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=4362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=4362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}