Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i get my code to run the method? 1

Status
Not open for further replies.

borntorun

MIS
Oct 16, 2003
82
GB
Hi Guys,

I don't want everything in main and i don't want to use static variable, how can i get this code to run. Thanks Man. Thanks for helping someone who is struggling away doing self-study.

import java.sql.*;

public class TestJdbc {

private static java.sql.Connection con = null;
public String url = "jdbc:microsoft:sqlserver://";
public String serverName= "localhost";
public String portNumber = "1433";
public String databaseName= "test";
public String userName = "test";
public String password = "password";

TestJdbc(){};
Connection GetConnect(){
//myDbTest.displayDbProperties();
//TestBrioJdbc myDbTest = new TestBrioJdbc();
String getConnectionUrl = url+serverName+":"+portNumber+";databaseName="+databaseName+";";



try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = java.sql.DriverManager.getConnection(getConnectionUrl,userName,password);
if(con!=null) System.out.println("Connection Successful!");
}catch(Exception e){
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return con;

}




public static void main(String[] args) {


new TestJdbc();

}

}
 
Change your main method to :

Code:
public static void main(String[] args) {
  TestJdbc test = new TestJdbc();
  Connection connection = test.GetConnect();
  // Do what you want with connection...
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top