Posts

Showing posts with the label MySQL

How to upload csv file into MySQL Database Example

Comma-Seperated-Value(CSV) file is a common file that used by most developers to extract data from database. Today i want to show how to upload or insert CSV file into database MySQL directly without need to read line by line in the CSV file. Let say this is the example of CSV file CSV File Content,Example 1 NO;DATE;TIME;ACCNO;STATUS 1;20131211;100856;12345;ACTIVE 2;20131211;095625;25896;NOT ACTIVE CSV File Content, Example 2 "NO";"DATE";"TIME";"ACCNO";"STATUS" "1";"20131211";"100856";"12345";"ACTIVE" "2";"20131211";"095625";"25896";"NOT ACTIVE" The above csv file are two example of csv files content. Lets start learn how to upload this file into MySQL Database. MySQL have a powerful sql statement to read this file and insert into database as batch. This statement is the fastest way to insert into database. You can read more about LOAD

JDBC PrepareStatement Insert Data As A Batch - JAVA(MySQL)

In this tutorial i will show example to add data into database MySQL as a batch. The idea is to prepare the batch data to pump into database one time only. This approach is a best solution if you more than 1 records to insert into database. If you insert one by one data to database, the process will be slow, hence batch process will inprove application performance. Take a look on the example and try it by your self. =) The Batch Update Example public void insertAsBatch()     {         String strsql = "INSERT INTO sysusers(UserID,UserEmail,Password) VALUES(?,?,?)";         PreparedStatement prepStatement = null;         Connection dbConn = TestConnectionDb();         int batchSize = 100;//set the batch size to commit intodatabase(optional)         int IndexCounterBatch = 0;         try         {                        dbConn.setAutoCommit(false);             prepStatement = dbConn.prepareStatement(strsql);             for(int i=0; i < 200;i++)             {                 

JDBC Prepare Statement to select data from database MySQL

In this tutorial i will show how to read data from database MySQl using PrepareStatement. You can read in this tutorial to create connection to database MySQL Full Example :  public void readDatabase()     {         String strsql = "SELECT * FROM sysusers WHERE UserID=?";         PreparedStatement prepStatement = null;         Connection dbConn = TestConnectionDb();         String uid = "";         String uemail = "";         try         {                        prepStatement = dbConn.prepareStatement(strsql);             prepStatement.setString(1, "developersnote");             ResultSet rs = prepStatement.executeQuery();             while (rs.next()) {                 uid = rs.getString("UserID");                 uemail = rs.getString("UserEmail");             }             System.out.println(uid);             System.out.println(uemail);         }         catch (SQLException se) {             System.out.println(se.getMessage

JDBC create connection or test connection to MySQL database

Here is a example how to make connection to database MySQL using ( J ava D ata B ase C onnectivity)JDBC driver. In your project you must include MySQL JDBC Driver, otherwise you will get error "MySQL JDBC Driver not found" . This is the full Method to check or test connection to MySQL Database using JDBC driver. The TestConnectionDb Method public static boolean TestConnectionDb() {                 String DbConnectionString ="jdbc:mysql://localhost:3306/";         //or you can directly connecto to your database schema like this :         //String DbConnectionString ="jdbc:mysql://localhost:3306/<schema name>";                 String User = "root";         String Password = "P@ssw0rd";                 Connection connection = null;         try {                        Class.forName("com.mysql.jdbc.Driver");             connection = DriverManager.getConnection(DbConnectionString, User, Password);         } catch (ClassNotFou