This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with connecting to a SQLServer database

 
Dinesh Jayaraj
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear friends:
i have written a gui to access a database and to excute a query entered by the user.(just for reference code is listed below) which works nicely.
Now iam trying to connect to a SQLServer database which is in the WindowsNT server to which my machine is connected. in the
local machine i have set the userDSN(using msODBC32 driver in the control panel)to the database in the server.
when run the pgm here is the exception iam getting.
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'INFORMINDIA\DINESH'.
the driver i have loaded: sun.jdbc.odbc.JdbcOdbcDriver
the url to getConnection: jdbc dbc:mahesh1
where mahesh1 is the userDSN specifying the database at WindowsNT server.
Iam not able to find out what is the problem?
is there any problem with url or driver?
The same pgm if i used to access the MSAccess db on the same server it works nicely for which driver is same
and url:-jdbc dbc:mahesh where mahesh dsn refers to MSaccess db on the server.
thanks in advance
dinesh
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class AccessApp extends Frame {
TextField driver = new TextField(60);
TextField url = new TextField(60);
TextField sql = new TextField(60);
Button doIt = new Button("Do it!");
TextArea resultArea = new TextArea(10,60);
public static void main(String args[]){
AccessApp app = new AccessApp();
}
public AccessApp() {
super("AccessApp");
setup();
pack();
addWindowListener(new WindowEventHandler());
show();
}
void setup() {
setupMenuBar();
setLayout(new GridLayout(2,1));
Panel topPanel = new Panel();
topPanel.setLayout(new GridLayout(4,1));
Panel panels[]=new Panel[4];
for(int i=0;i<panels.length;++i){>
panels[i]=new Panel();
panels[i].setLayout(new FlowLayout(FlowLayout.LEFT));
}
panels[0].add(new Label("Driver:"));
panels[0].add(driver);
panels[1].add(new Label("URL: "));
panels[1].add(url);
panels[2].add(new Label("SQL: "));
panels[2].add(sql);
doIt.addActionListener(new ButtonHandler());
panels[3].add(doIt);
for(int i=0;i<panels.length;++i)>
topPanel.add(panels[i]);
add(topPanel);
add(resultArea);
}
void setupMenuBar() {
MenuBar menuBar = new MenuBar();
Menu fileMenu = new Menu("File");
MenuItem fileExit = new MenuItem("Exit");
fileExit.addActionListener(new MenuItemHandler());
fileMenu.add(fileExit);
menuBar.add(fileMenu);
setMenuBar(menuBar);
}
void accessDB() {
try{
// Load JDBC driver
Class.forName(driver.getText());
// Connect to database
Connection connection=DriverManager.getConnection(url.getText());
Statement statement = connection.createStatement();
// Execute SQL
boolean hasResults = statement.execute(sql.getText());
if(hasResults){
// Get results of query
ResultSet result = statement.getResultSet();
if(result!=null) displayResults(result);
}else resultArea.setText("");
// Close database connection
connection.close();
}catch(Exception ex){
resultArea.setText(ex.toString());
}
}
void displayResults(ResultSet r) throws SQLException {
ResultSetMetaData rmeta = r.getMetaData();
// Use meta data to obtain the number of columns
int numColumns=rmeta.getColumnCount();
String text="";
for(int i=1;i<=numColumns;++i) {
if(i<numColumns)>
text+=rmeta.getColumnName(i)+" | ";
else
text+=rmeta.getColumnName(i);
}
text+="\n";
while(r.next()){
for(int i=1;i<=numColumns;++i) {
if(i<numColumns)>
text+=r.getString(i)+" | ";
else
text+=r.getString(i).trim();
}
text+="\n";
}
resultArea.setText(text);
}
class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent ev){
String s=ev.getActionCommand();
if(s=="Do it!") accessDB();
}
}
class MenuItemHandler implements ActionListener {
public void actionPerformed(ActionEvent ev){
String s=ev.getActionCommand();
if(s=="Exit"){
System.exit(0);
}
}
}
class WindowEventHandler extends WindowAdapter {
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}

[This message has been edited by Dinesh Jayaraj (edited December 14, 2000).]
[This message has been edited by Dinesh Jayaraj (edited December 14, 2000).]
 
NageswaraRao Karra
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai!
is it not the problem of username and password?
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
instead of:

you should use:

where userId and password are set to your SQL Server login id and password.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic