Skip to Content
0
Former Member
Apr 20, 2007 at 07:15 AM

How to connect database

14 Views

This is my jsp file!(







Claims Processing: Data Entry

  • Created on Apr 18, 2007

*

  • To change the template for this generated file go to

  • Window>Preferences>Java>Code Generation>Code and Comments

*/

package Database;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.sql.DataSource;

/**

  • @author Administrator

*

  • To change the template for this generated type comment go to

  • Window>Preferences>Java>Code Generation>Code and Comments

*/

public class DataRetriever {

Connection connection = null;

public DataRetriever(String jndiName) {

try {

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

connection =

DriverManager.getConnection(

"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=HRMS;User=sa;Password=sa");

System.err.println("Database connection established AGAIN");

} catch (Exception e) {

System.err.println("Cannot connect to database server - " + e);

}

}

private void getConnection(String jndi) {

try {

Context ctx = new InitialContext();

if (ctx == null) {

throw new Exception("Boom - No Context");

}

DataSource ds = (DataSource) ctx.lookup("java:comp/env/" +jndi);

System.err.println(jndi);

if (ds != null) {

connection = ds.getConnection();

}

} catch (Exception e) {

e.printStackTrace();

connection = null;

}

}

public ResultSet execSelectQuery(String sql) {

try {

if (connection == null) {

return null;

}

Statement s = connection.createStatement();

return s.executeQuery(sql);

} catch (SQLException e) {

e.printStackTrace();

return null;

}

}

public int execUpdateQuery(String sql) {

try {

if (connection == null) {

return -1;

}

Statement s = connection.createStatement();

return s.executeUpdate(sql);

} catch (SQLException e) {

e.printStackTrace();

return -1;

}

}

public void terminate() {

try {

connection.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

This is the servlet.
package servlet;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.createClaimsBean;

public class CreateClaimsServlet extends HttpServlet {
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

private void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
String mySqlJndiName =
this.getServletContext().getInitParameter("MySql JNDI");
createClaimsBean cb = new createClaimsBean();

String bd = request.getParameter("textfield1");
String pl = request.getParameter("textfield2");
String pa = request.getParameter("textfield3");
String bn = request.getParameter("textfield3");
String d = request.getParameter("D1");
String ta = request.getParameter("textfield4");

System.out.println("n");
cb=cb.registerCreateClaims(bd, pl, pa, bn, d, ta, mySqlJndiName);
request.getSession().setAttribute("registerCreateClaims", cb);

request.getRequestDispatcher("/WEB-INF/pages/regsuccess.jsp").forward(request,
response);
}
}

Lastly it is the java file.

/*

  • Created on Apr 19, 2007

*

  • To change the template for this generated file go to

  • Window>Preferences>Java>Code Generation>Code and Comments

*/

package bean;

import Database.DataRetriever;

/**

  • @author Administrator

*

  • To change the template for this generated type comment go to

  • Window>Preferences>Java>Code Generation>Code and Comments

*/

public class createClaimsBean {

private String billDate;

private String plans;

private String panel;

private String billNo;

private String dependent;

private String totalAmt;

public createClaimsBean(String billDate,String plans,String panel,String billNo,String dependent,String totalAmt)

{

this.billDate = billDate;

this.plans = plans;

this.panel = panel;

this.billNo = billNo;

this.dependent = dependent;

this.totalAmt = totalAmt;

}

/**

  • @return

*/

public String getBillDate() {

return billDate;

}

/**

  • @return

*/

public String getBillNo() {

return billNo;

}

/**

  • @return

*/

public String getDependent() {

return dependent;

}

/**

  • @return

*/

public String getPanel() {

return panel;

}

/**

  • @return

*/

public String getPlans() {

return plans;

}

/**

  • @return

*/

public String getTotalAmt() {

return totalAmt;

}

/**

  • @param string

*/

public void setBillDate(String string) {

billDate = string;

}

/**

  • @param string

*/

public void setBillNo(String string) {

billNo = string;

}

/**

  • @param string

*/

public void setDependent(String string) {

dependent = string;

}

/**

  • @param string

*/

public void setPanel(String string) {

panel = string;

}

/**

  • @param string

*/

public void setPlans(String string) {

plans = string;

}

/**

  • @param string

*/

public void setTotalAmt(String string) {

totalAmt = string;

}

public createClaimsBean{

public void registerCreateClaims(String mysql, String billNo, String dependent, String panel, String plans, String totalAmt)

{

DataRetriever data = new DataRetriever(mysql);

String sql= "INSERT INTO claims VALUES (billDate, billNo, dependent, panel, plans, totalAmt)";

data.execUpdateQuery(sql);

data.terminate();

}

}

}

How to connect sql so that i m able to insert new data into the database?

Message was edited by:

fatimah rasheed