Can anyone help with this code please originally. I want to connect to SQL Server 2000 database I already installed the driver and set the classpath for it + put the jar file inside
tomcat and I am
testing using MS ACcess dataabse.
I am trying to connect to A SQL Server database with a login name and password and to change my username and password through
JSP.
The MSAccess database code
import java.sql.*;
public class ClassConnect{
Connection conn;
Statement stmt ;
ResultSet rs;
//setup the database connection here, once you change you username and passwd,
//you do not need to change it for any table
public static void main(
String [] args){
public void DbInit(){
try{
Class c = Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
/* change user name and password here */
String url = "jdbc

dbc:Usuario";
//conn = DriverManager.getConnection( url, "eloi.teixeira","eloi197525" );
conn = DriverManager.getConnection("jdbc.Odbc.DRIVER=Microsoft Access Driver (*.mdb);DBQ=c:/Usuario.mdb");
System.out.println("ok");
stmt = conn.createStatement();
}//try
catch(SQLException e){System.out.println("ok");}
}//dbInit
}//main
public boolean updateTable(String update_sql){
PreparedStatement ps = null;
try {
ps=conn.prepareStatement(update_sql);
ps.executeUpdate();
} catch (SQLException e) { return false; }
return true;
}
public ResultSet queryTable(String query_sql){
try{
Statement stmt = conn.createStatement();
return stmt.executeQuery(query_sql);
}catch(SQLException e){return null;}
}
public void DbClose(){
try{
conn.close();
}
catch(SQLException e){}
}
}
This is to connect with SQL Server:
import java.sql.*;
import java.util.*;
public class ClassConnect{
Connection conn;
Statement stmt ;
ResultSet rs;
public void DbInit(){
try{
DriverManager.registerDriver(com.microsoft.jdbc.sqlserver.SQLServeR);
String url = "com.jdbc:microsoft:sqlserver:http:\\localhost:8080

atabasename:tablename";
conn = DriverManager.getConnection( url, "username","password" );
stmt = conn.createStatement();
}catch(SQLException e){}
}
public boolean updateTable(String update_sql){
PreparedStatement ps = null;
try {
ps=conn.prepareStatement(update_sql);
ps.executeUpdate();
} catch (SQLException e) { return false; }
return true;
}
public ResultSet queryTable(String query_sql){
try{
Statement stmt = conn.createStatement();
return stmt.executeQuery(query_sql);
}catch(SQLException e){return null;}
}
public void DbClose(){
try{
conn.close();
}
catch(SQLException e){}
}
}
The Jsp file is:
<%@ page import = "java.sql.*, ClassConnect" %>
<jsp:useBean id="myCBean" class="ClassConnect" scope="session"/>
<html>
<head><title>
JDBC</title></head>
<body>
<% myCBean.DbInit(); %>
Database connection is initialized successfully! <hr>
<% } %>
<% if (myCBean.updateTable("update WIZUSUARIOS set valor = eloi.campos where UID = 'eloi.teixeira' ")) { %>
Insert data into the new table successfully!<hr>
<% } else { %>
Cannot insert data into wizusuarios table!<br>
<% } %>
<% if (myCBean.updateTable("Update WIZUSUARIOS set valor = eloi197529 where PWD = 'eloi197525' ")) { %>
Insert data into the new table successfully!<hr>
<% } else { %>
Cannot insert data into the table!<br>
<% } %>
<% ResultSet rs = myCBean.queryTable("SELECT * FROM tablename"); %>
<% while (rs.next()){ %>
<%= rs.getInt(1) %>
<%= rs.getString(2) %>
<% } %>
<hr>
<% myCBean.DbClose(); %>
Database connection is closed!
</body>
</html>
Any help would be great.
Thanks