/* first of all you Must Create a Database Inventory ans Sales Table
import java.sql.*;
import java.io.File;
public class sconnect
{
public static void main(String[] args)
{
try
{
// Load MS accces driver class
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
File db=new File("inventory.accdb");
String path=db.getAbsolutePath();
// C:\\databaseFileName.accdb" - location of your database
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+path+";";
// specify url, username, pasword - make sure these are valid
Connection con = DriverManager.getConnection(url, "username", "password");
System.out.println("Connection Succesfull");
Statement st=con.createStatement();
String iname="Compass Box";
int sales_id=12;
float price=48.75f;
int sales=5;
float total=price*sales;
String sql,query;
int ret;
sql="insert into sales values("+sales_id+",'"+iname+"',"+price+","+sales+","+total+");";
ret=st.executeUpdate(sql);
query="select * from sales";
ResultSet rs=st.executeQuery(query);
System.out.println("\nID "+"Item Name"+"\t\t"+" price"+"\t"+"Sales"+"\t"+"Total\n");
while(rs.next())
{
sales_id=rs.getInt("sales_id");
iname=rs.getString("iname");
price=rs.getFloat("price");
sales=rs.getInt("sales");
total=rs.getFloat("total");
System.out.printf("%6d %-20s %4.2f %8d %6.2f%n",sales_id,iname,price,sales,total);
//System.out.println(iname+"\t"+sales_id+"\t"+price+"\t"+sales+"\t"+total);
}
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
import java.sql.*;
import java.io.File;
public class sconnect
{
public static void main(String[] args)
{
try
{
// Load MS accces driver class
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
File db=new File("inventory.accdb");
String path=db.getAbsolutePath();
// C:\\databaseFileName.accdb" - location of your database
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+path+";";
// specify url, username, pasword - make sure these are valid
Connection con = DriverManager.getConnection(url, "username", "password");
System.out.println("Connection Succesfull");
Statement st=con.createStatement();
String iname="Compass Box";
int sales_id=12;
float price=48.75f;
int sales=5;
float total=price*sales;
String sql,query;
int ret;
sql="insert into sales values("+sales_id+",'"+iname+"',"+price+","+sales+","+total+");";
ret=st.executeUpdate(sql);
query="select * from sales";
ResultSet rs=st.executeQuery(query);
System.out.println("\nID "+"Item Name"+"\t\t"+" price"+"\t"+"Sales"+"\t"+"Total\n");
while(rs.next())
{
sales_id=rs.getInt("sales_id");
iname=rs.getString("iname");
price=rs.getFloat("price");
sales=rs.getInt("sales");
total=rs.getFloat("total");
System.out.printf("%6d %-20s %4.2f %8d %6.2f%n",sales_id,iname,price,sales,total);
//System.out.println(iname+"\t"+sales_id+"\t"+price+"\t"+sales+"\t"+total);
}
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
Java Access Database Connectivity without DSN
Reviewed by Unknown
on
9:13 am
Rating:
No comments: