Connect Java with MySql

Pre-requisite:

1.Java installed.

2.WAMP(Windows Apache MySql PHP) installed.

Few Steps to Connect your Java Program with your MySql Backend.

Step 1:

Check that your Computer has Java Installed.

Step 2:

Download the MySql Connection for Java.

Step 3:

Put the downloaded JAR(Java Archive File) in to the location where JavaHome/jdkversion/jre/lib/ext ..

Inside to the ext Directory.

Step 4:

Write Java Code to Connect to the MySql Database.

Sample code to connect Java with MySql.

File:jdbc.java

import java.sql.*;
class jdbc
{
public static void main(String ar[])
{
try
{
Class.forName (“com.mysql.jdbc.Driver”);
Connection con=DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/databasenamehere”,”root”,””);
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(“select * from tablenamehere”);
while(rs.next())
{
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
}
catch(Exception e)
{
System.out.println(“Error occurerd”);
}
}
}

Step 5:

Compile your Java using javac filename.java.

On the Successful Completion of Java File you will see that the Class file has been created.

Run your Java File using java filename.

You will get Data from the MySql Database.

Feel free to share your comments and your Queries.

Advertisements

Chat Program in Java

Here is the sample program for chatting with our friends and rest of us. Fell free to see the code and just try it out. And please post your comments as well.

chat.java

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class chat extends JFrame implements ActionListener
{
JPanel panel;
JScrollPane jsp;
JTextField tf;
JButton button;
static JTextArea ta;
ConnectionThread conn;
public chat() throws Exception
{
super(“Chat”);
panel=new JPanel();
ta=new JTextArea(10,40);
jsp=new JScrollPane(ta);
tf=new JTextField(33);
button=new JButton(“Send”);
conn=new ConnectionThread();
ta.setEditable(false);
panel.add(jsp);
panel.add(tf);
panel.add(button);
button.addActionListener(this);
getContentPane().add(panel);
setSize(500,300);
setVisible(true);
}
static void printReply(String str)
{
ta.setText(ta.getText()+”REMOTE PORT:”+str.trim()+”\n”);
}
public static void main(String args[]) throws Exception
{
new chat();
}
public void actionPerformed(ActionEvent e)
{
try
{
ta.setText(ta.getText()+tf.getText()+”\n”);
conn.write(tf.getText());
tf.setText(“”);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
class ConnectionThread extends Thread
{
DatagramSocket dgs;
DatagramPacket dgp;
static byte[] buf;
int sourceport,destport;
String ipaddress;
public ConnectionThread() throws Exception
{
ipaddress=JOptionPane.showInputDialog(“Enter remote machine’s IP address: (Press cancel if using the local machine)”);
sourceport=Integer.parseInt(JOptionPane.showInputDialog(“Enter source port:”));
destport=Integer.parseInt(JOptionPane.showInputDialog(“Enter destination port:”));
dgs=new DatagramSocket(sourceport);
buf=new byte[256];
dgp=new DatagramPacket(buf,buf.length);
start();
}
public void run()
{
try
{
while(true)
{
dgs.receive(dgp);
String received=new String(dgp.getData(),0);
chat.printReply(received);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public void write(String str) throws Exception
{
buf=new byte[255];
str.getBytes(0,str.length(),buf,0);
DatagramPacket dgp=new DatagramPacket(buf,buf.length,InetAddress.getByName(ipaddress),destport);
dgs.send(dgp);
}
}