Tuesday 2 July 2013
How to create SqlBean class for Set and Get Query in java
public class SQLBeanTest {
public static void main(String args[]){
SQLBean testBean = new SQLBean();
try{
testBean.setClassname("net.sourceforge.jtds.jdbc.Driver");
testBean.setUrl("jdbc:jtds:sqlserver://192.168.7.128:1433/COMPANY;user=sa;password=ABCD;appName=COMPANY;progName=Ramsis");
//testBean.setClassname("com.mysql.jdbc.Driver");
//testBean.setUrl("jdbc:mysql://192.168.7.128:3306/gts;user=root;password=ABCD;");
testBean.connect();
testBean.setAutoCommit(false);
//String[][] rsResult =
testBean.getSPResultSet("{call_RTE(?,?,?,?,?,?,?)}", 1, 25);
testBean.setQuery("select * from dbo.Atm_2009");
//testBean.setQuery("select * from roadways_busstop");
testBean.execute();
testBean.commit();
testBean.close();
System.out.println("Test Complete...");
}
catch (Exception ex){
System.out.println(ex);
}
}
}
//*************************************************************************//
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
public class SQLBean {
String pr;
String pr1;
String classname;
String url;
String username;
String password;
String query;
ResultSet rs = null;
Vector result;
Connection con;
String[] tableNames;
int[] columnTypes;
String[][] resultArray;
String temp_catch_val = "";
String strError;
Vector vcParam;
PreparedStatement pstmt = null;
public void setClassname(String classname)
{
this.classname = classname;
}
public String getClassname()
{
return this.classname;
}
public void setUrl(String url)
{
this.url = url;
}
public String getUrl()
{
return this.url;
}
public void setUsername(String username)
{
this.username = username;
}
public String getUsername()
{
return this.username;
}
public void setPassword(String password)
{
this.password = password;
}
public String getPassword()
{
return this.password;
}
public void setQuery(String query)
{
this.query = query;
}
public String getQuery()
{
return this.query;
}
public void setAutoCommit(boolean bState)
throws SQLException
{
this.con.setAutoCommit(bState);
}
public boolean getAutoCommit()
throws SQLException
{
return this.con.getAutoCommit();
}
public void commit()
throws SQLException
{
this.con.commit();
}
public void rollback()
throws SQLException
{
this.con.rollback();
}
public void close()
throws SQLException
{
if (this.con != null)
this.con.close();
}
public void connect()
throws ClassNotFoundException, SQLException
{
Class.forName(this.classname);
this.con = DriverManager.getConnection(this.url, this.username,
this.password);
}
public Vector execute()
throws SQLException
{
Statement stmt = (Statement) this.con.createStatement();
rs = stmt.executeQuery(this.query);
while(rs.next()){
System.out.println(rs.getString(3));
//System.out.println(rs.getString(5));
}
return result;
}
public String toString(){
return "result"+result;
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment