How to connect to the mysql database in java

To connect to a mysql database we need the following details about the database. Driver class- driver class for mysql which is " ...

To connect to a mysql database we need the following details about the database.

  • Driver class-driver class for mysql which is "com.mysql.jdbc.Driver".
  • Connection URL-connection url looks like "jdbc:mysql://localhost:3306/pointofsale where jdbc is the API ,mysql  is database type ,localhost is the server name which may also be ip address where the mysql database is running  and 3306 is the port number  and pointofsale database name
  • username-default username for mysql server is root
  • password- password is given by the user when installing the mysql database.

Example code for connecting  to mysql database in java

Before running below code,ensure you have mysql server installed in your computer.Create database pointofsale.


import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class DbConnection {
  public Connection con;
DbConnection()
{
try
{
Class.forName("com.mysql.jdbc.Driver"); 
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/pointofsale","root","");
Statement stmt=con.createStatement();  
ResultSet rs=stmt.executeQuery("select * from users");  
while(rs.next())  
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+" "
        + ""+rs.getString(4)+" "+rs.getString(5)+" "
                + ""+rs.getString(6));  
con.close();  
{
}

}
catch(Exception e)
{
    e.printStackTrace();

}
}

public static void main(String args[])
{
new DbConnection();
}
}

Output

COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: How to connect to the mysql database in java
How to connect to the mysql database in java
https://1.bp.blogspot.com/-_qrno1kNFXM/WWvnlI-24-I/AAAAAAAAITU/pLRczBMzGAc1oClb6tZP61Tt-r1aIddWACLcBGAs/s320/How%2Bto%2Bconnect%2Bto%2Bthe%2Bmysql%2Bdatabase%2Bin%2Bjava-min.PNG
https://1.bp.blogspot.com/-_qrno1kNFXM/WWvnlI-24-I/AAAAAAAAITU/pLRczBMzGAc1oClb6tZP61Tt-r1aIddWACLcBGAs/s72-c/How%2Bto%2Bconnect%2Bto%2Bthe%2Bmysql%2Bdatabase%2Bin%2Bjava-min.PNG
Java Codes
http://www.download-all.net/2017/07/how-to-connect-to-mysql-database-in-java.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/07/how-to-connect-to-mysql-database-in-java.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy