Connect Java Program to mysql database

step 1: create mysql Database and tables

create database studinfo;

use studinfo;

create table stud(id int(10), name varchar(20), dept varchar(20), phn int(12));

step 2: create java program

javaconnectivity
Step 3: To Execute this program you require mysql connector

Download from here….
mysql Connector

Step 4: Configure Eclipse

1.1 Start Eclipse ->  File -> New Project -> Give any name to project -> finish

CreateProject

1.2 right click on your project in package explorer ->new-> class-> give class name and optional package name

newclass

Note: your Class name should always start with “Uppercase letter”.

1.3 copy following code and and Paste in your java class created above

package studinfo;

import java.sql.*;
class StudInfoDatabaseConnection
{
public static void main(String args[]){
try{
Class.forName(“com.mysql.jdbc.Driver”);
Connection con=DriverManager.getConnection
( “jdbc:mysql://localhost:3306/studinfo”,”root”,”root”);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(“select * from stud”);
while(rs.next())
System.out.println (rs.getInt(1)+” “+rs.getString(2)+” “+rs.getString(3)+ ” ” + rs.getInt(4));
con.close();
}
catch(Exception e){ System.out.println(e);
}
}

1.4 right click ->your project in package explorer ->Buid path -> Configure Build path -> libraries

buidpath

Select your mysql connector Downloded in step 3 above -> Click ok -> Run Project.

SDL Lab Assignments

Data Structures in java

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.Stack;
import java.util.Vector;

 

public class DataStructures {

enum Days //to define fixed set of constants static and final
{
Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday;

}
public static void main(String[] args) throws IOException
{
Enumeration days; //to iterate elements in a collection of objects
Vector dayNames = new Vector();
dayNames.add(“Sunday”);
dayNames.add(“Monday”);
dayNames.add(“Tuesday”);
dayNames.add(“Wednesday”);
dayNames.add(“Thursday”);
dayNames.add(“Friday”);
dayNames.add(“Saturday”);
days = dayNames.elements();

while(days.hasMoreElements())
{
System.out.println(days.nextElement());
}

Days day=Days.Monday;

switch(day)
{
case Sunday :
System.out.println(“sunday”);
break;
case Monday :
System.out.println(“monday”);
break;
}

//bit set special type of array that holds bit values can increase in size as needed.

BitSet bits1 = new BitSet();
BitSet bits2 = new BitSet();

for(int i=1;i<=10;i++)
{
if(i%2==0)
bits1.set(i);
else
bits2.set(i);
}

System.out.println(bits1);
System.out.println(bits2);

//and op
System.out.print(“bits and”);
bits2.and(bits1);
System.out.println(bits2);

//or op
System.out.print(“bits or”);
bits2.or(bits1);
System.out.println(bits2);

//vactor
dynamic array
Vector<String> v=new Vector<>();
v.add(“hi”);
v.add(“hello”);
v.add(“ok”);

for(String s:v)
{
System.out.println(s);
}

//stack

Stack st=new Stack();
st.push(“hi”);
st.push(“hello”);
st.push(“bye”);

System.out.println(st.pop());
System.out.println(st);

//dictionary

//key value
Dictionary d = new Hashtable();
d.put(1, “sunday”);
d.put(2, “monday”);
d.put(3, “tuesday”);
d.put(4, “wednesday”);
d.put(5, “thursday”);
d.put(6, “friday”);
d.put(7, “saturday”);

for(int i=1;i<d.size();i++)
{
System.out.println(d.get(i));
}

Enumeration e1 = d.keys();
Enumeration e2 = d.elements();
while(e1.hasMoreElements())
{
System.out.println(e1.nextElement());
}
while(e2.hasMoreElements())
{
System.out.println(e2.nextElement());
}
//dictionary -> HashTable ->Properties

//properties key value both strings

Properties capitals = new Properties();
capitals.put(“India”, “Delhi”);
capitals.put(“United States”, “Washington D.C.”);
capitals.put(“United Kingdom”, “London”);
capitals.put(“Japan”, “Tokyo”);
capitals.put(“Thailand”, “Bangkok”);
capitals.put(“France”, “Paris”);
capitals.put(“Germany”, “Berlin”);

Set keys=capitals.keySet();
Iterator itr = keys.iterator();

while(itr.hasNext()) {
String str = (String) itr.next();
System.out.println(“The capital of ” + str + ” is ” +
capitals.getProperty(str) + “.”);
}

capitals.store(new FileWriter(“info.properties”),”Properties Example”);

FileReader reader=new FileReader(“info.properties”);

Properties p=new Properties();
p.load(reader);

System.out.println(p.getProperty(“India”));
System.out.println(p.getProperty(“United States”));
}
}