Tuesday, April 19, 2011

sqlite.JDBC Sample code..

//run by
//java -classpath ".;sqlite-jdbc-3.5.7.jar" Sample

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


class Test
{

String path="jdbc:sqlite:C:/Documents and Settings/Dipankar/Desktop/Dropbox/mq/sample.db";

//=======================logic strat from here add user..===================[DONE]

public void adduser(String username,String filename,String content)
{

// load the sqlite-JDBC driver using the current class loader
Connection connection = null;



//auto calculete id
int uid=0;
try{
connection = DriverManager.getConnection(path);
Statement statement1 = connection.createStatement();
ResultSet rs1=statement1.executeQuery("select count(*) from userinfo");
uid=rs1.getInt(1)+1;
}
catch(Exception e)
{
System.out.println("Error here");
}
finally
{
try
{
if(connection != null)
connection.close();
}

catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}
System.out.println("insert into userinfo values("+uid+",'"+username+"','"+filename+"','"+content+"')");









try
{

connection = DriverManager.getConnection(path);
Statement statement = connection.createStatement();

statement.executeUpdate("insert into userinfo values("+uid+",'"+username+"','"+filename+"','"+content+"')");
}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
}
finally
{
try
{
if(connection != null)
connection.close();
}

catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}



}
//=======================END==================================================================





//=======================logic strat from here view user..===================[DONE]
public void showinfo(int userid)
{



// load the sqlite-JDBC driver using the current class loader
Connection connection = null;


try
{

connection = DriverManager.getConnection(path);
Statement statement = connection.createStatement();

ResultSet rs = statement.executeQuery("select * from userinfo where userid="+userid);
while(rs.next())
{
// read the result set
System.out.println("id = " + rs.getInt("userid"));
System.out.println("name = " + rs.getString("username"));
System.out.println("finemane = " + rs.getString("filename"));
System.out.println("file conetct = " + rs.getString("filecontent"));

}

}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
}
finally
{
try
{
if(connection != null)
connection.close();
}

catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}



}
//=======================END===================================================================


//=======================logic strat from here view user..===================[DONE]
public void removeuser(int userid)
{

// load the sqlite-JDBC driver using the current class loader
Connection connection = null;


try
{

connection = DriverManager.getConnection(path);
Statement statement = connection.createStatement();

statement.executeUpdate("delete from userinfo where userid="+userid);

}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
}
finally
{
try
{
if(connection != null)
connection.close();
}

catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}

}

//=======================END===================================================================


}

public class TestDB
{


public static void main(String[] args) throws ClassNotFoundException
{
// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");

Test x= new Test();
x.showinfo(0);
}
}

No comments:

Post a Comment