Friday 11 March 2016

Connecting Mysql Database in NetBeans

Web hosting
Next -> http://dnetbeans.blogspot.in/p/in-last-session-we-learned-about-how-to.html

First for connecting mysql database in netbeans you need to do some things https://netbeans.org/kb/docs/ide/mysql.html Visit this site to know detailed information.

Alright,
Then for programming you need to download a jar file
http://www.java2s.com/Code/JarDownload/mysql/mysql-connector-java-5.1.23-bin.jar.zip
This is direct download link.

All set !!,

1.Then create a new java project.
2.Right click your project and go to properties->library->add jar/folder. Locate to your downloaded      jar file and then click open.
3.Create a java class in your project.Name the class Databaseconn
4.Type in the following code, eg. jdbc:mysql://localhost:integer/yourdatabasename


import java.sql.*;
import javax.swing.*;
public class Databaseconn {
Connection conn = null;
    static Connection ConnectDb() {
        try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/sahil","root","host");//Type your database host name here
            JOptionPane.showMessageDialog(null,"Connection Established");
            return conn;
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
     
    }

}

*Note that i have not wrote the same name as given in my database.
The "root" and "" is my username and password.

5.Now create a jframe form and add a button, name it connect.
6.Open the code and type the following code

7.Now go to the design tab and then double click the button Or RightClick->Events->MouseClick
   Type the following code:
     conn = Databaseconn.ConnectDb();



8.Run the file and click connect.

No comments:

Post a Comment