Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Forums : J2EE : JSP : i have an error : db connect within a javabean Signup · Login
Author Thread
i have an error : db connect within a javabean
Posted in tutorial: What are JavaBeans ?
·  shibby1011ph
User
Joined: 20 Aug 2003
Total Posts: 7
i have an error : db connect within a javabean
Posted: 5 Sep 2003
package Pimms;

import java.sql.*;
import java.io.*;
import java.util.*;

public class Payroll {

String dbURL = "jdbc:mysql://127.0.0.1/JSP_DB1?user=root&password=";
String dbDriver = "com.mysql.jdbc.Driver";
String query = "";
int r = 0;
private Connection dbCon;

public Payroll(){
super();
}

public boolean connect() throws ClassNotFoundException,SQLException
{ Class.forName(dbDriver);
dbCon = DriverManager.getConnection(dbURL);
return true;
}

public void close() throws SQLException
{ dbCon.close();
}

public ResultSet execSQL(String sql) throws SQLException
{ Statement s = dbCon.createStatement();
ResultSet r = s.executeQuery(sql);
return (r == null) ? null : r;
}

public int updateSQL(String sql) throws SQLException
{ Statement s = dbCon.createStatement();
r = s.executeUpdate(sql);
return r;
}

public String getTimeIn(int emp_id, String date) throws SQLException
{ String time = "NONE";
query = "SELECT time FROM ATTENDANCE WHERE inout=1 AND emp_id="+emp_id+" AND date='"+date+"'";

// THIS IS WHERE MY PROBLEM LIES.
// USING THE CODE BELOW, I CAN CONNECT TO MY DB WITHOUT ANY PROBLEMS
// BUT IF U LOOK AT THE UPPER PORTION OF MY CODE, YOU WILL SEE METHODS
// NAMED connect(), execSQL(), updateSQL()
// I CREATED THESE 3 METHODS SO I WOULDN'T HAVE TO DECLARE THE dbCon
// (AMONG OTHER THINGS), the statement, the resulset OVER AND OVER AGAIN.
// BUT IF I PUT this: dbCon.connect();
// THIS ERROR APPEARS : interface java.sql.Connection
// please help... i would like to use as much java beans as possible
try
{ Class.forName(dbDriver).newInstance();
dbCon = DriverManager.getConnection(dbURL);

Statement s = dbCon.createStatement();
ResultSet r = s.executeQuery(query);

if(r.next())
time = r.getString("time");
r.close();
dbCon.close();
}catch(Exception e)
{ e.printStackTrace(); }

return time;
}
}

Just a JSP Newbie =)

Users Who Have Visited This Thread In Last 24 Hours
2 Visitors

Login
UserName Or Email Address:       Password:       Auto-Login:    
· Create New User Account
· Send Forgotten Password by Email
 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.