How do you insert data into a database (MSSQL) directly from a JSP - or do i need a bean ?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   al_still
Posted On:   Tuesday, September 4, 2001 02:18 AM

Could anyone tell me how to insert data into a database (MSSQL) directly from a JSP.
I've tried using a select inside my JSP but i can only insert literals, not variables,
although the SQLException caught in my webServer states that only expressions,variables allowed not column names.
But my var names and col names ARE different eg. fName (var) firstname (col). Any ideas ?



Thanx in advance.

Re: How do you insert data into a database (MSSQL) directly from a JSP - or do i need a bean ?

Posted By:   Ivo_Limmen  
Posted On:   Tuesday, September 4, 2001 02:45 AM

Using a bean is more readable for other programmers. You should try not to mix Java and HTML into a JSP too much.
To insert som data into a database use this:

Connection c = null;
PreparedStatement ps = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // load the driver (should be a differend one)
c = DriverManager.getConnection("jdbc:odbc:DB", "admin", "password");
ps = c.prepareStatement("insert into MyTABLE (NAME) VALUES ('Ivo!')");
ps.execute();
} catch(Exception ex) {
System.out.println("Error: "+ex.getMessage());
} finally {
try {
if(ps != null) {
ps.close();
}
if(c != null) {
c.close();
}
ps = null;
c = null;
} catch(Exception ex) {
System.out.println("Error: "+ex.getMessage());
}
}
About | Sitemap | Contact