Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jdbc-jsp

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to connect my jsp prog with ms sql server.
If i want to pass the values which will be taken from jsp file & i want to put it in table,So what will be the command for that?
I want to pass the query as variable not the permanent values.
Plz help me out....
kshitija
 
kichu kichu
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai kshitija,
This is the class file for connecting to the sql database--->
package packagename;
import java.sql.*;
public class getconnection
{
public Connection getconnection(String getdsnname)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbcdbc:"+getdsnname;
Connection con=DriverManager.getConnection(url,"username","password");
return con;
}
catch(ClassNotFoundException e)
{
System.out.println("class not found....");
return null;
}
catch(SQLException se)
{
System.out.println("unable to connect");
return null;
}
}

}
*****************************************************************Here is a part of the jsp file which inserts data--->

Here assume that u have got two text boxes in whichu enter value.
U name them as txt1 and txt2.(I have not added the html code)
After putting in values in the textboxes ,u submit the values(say to the same jsp page).The values in the text boxes are obtained in the variables strProjName and strProjStat.
These values are then inserted into the database.

<%
String conMfield=null;
Statement stmt=null;
ResultSet rs=null;
String sql="";
String strProjName;
String strProjStat;
try
{
getconnection gc=new getconnection();
Connection conMfield=gc.getconnection("DSN Name");
stmt=conMfield.createStatement();
sql="insert into table(Field1,Field2)values('"+strProjName+"','"+strProjStat+"')";
}
catch(SQLException e)
{
System.out.println("exception:"+e);

}

%>

I have given only a rough idea here for u.But hope this is of help to u.
If u have any problems,i will paste the entire code for you.
U can contact me at kichuhere@rediffmail.com
regards,
kichu.

[This message has been edited by kichu kichu (edited May 11, 2001).]
[This message has been edited by kichu kichu (edited May 11, 2001).]
[This message has been edited by kichu kichu (edited May 11, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic