Page 1 of 1

SQL Connection

Posted: 11 Apr 2006, 17:00
by bhangie
Hi

I am starting to learn java in particular creating web applications so i am a newbie @ this. Ok I need to create a connection to SQL Database in my
application, Using Netbeans 5 i downloaded the jdbc drivers installed and all is working fine, Problem comes when i need to create a connection in my app
Can someone plz post some code so i can see how it suppose to done. I did
get examples from the web.

Code: Select all

class DConnection {
           public static void main(String[] args) {
            try {
              DriverManager.registerDriver(
                 new com.microsoft.sqlserver.jdbc.SQLServerDriver());
                 //In this line i get Package not found
   
                 Connection connection = DriverManager.getConnection("jdbc:microsoft:sqlserver://PIAPE","sa","*****");
The right way to do this?
thanks in advance

Posted: 11 Apr 2006, 17:53
by Moses
Have you added this:
import java.sql.*; ?

Posted: 11 Apr 2006, 20:08
by Law
soz man if my pc wasnt broken i could give you the code.

I created a java program that connected to a sql db. i'll see if i can find nething.

Posted: 11 Apr 2006, 20:19
by Moses
public Main() {

try {

// Here we load the JDBC driver.

Class.forName("org.postgresql.Driver"); (whatever driver you need)

} catch (ClassNotFoundException e) {

System.out.println("Unable to load driver!");


}

try {

Con = DriverManager.getConnection("jdbc:microsoft:sqlserver://PIAPE","sa","*****");

} catch (SQLException se) {

System.out.println("SQL Exception: " + se.getMessage());

se.printStackTrace(System.out);

}
This is just code I wrote once... May help.

Posted: 12 Apr 2006, 11:19
by bhangie
Hi

Thanks for the help :)
@Moses your code works thanks man.

I Only had to do this

Code: Select all

Connection Con = DriverManger.getConnection....
Otherwise it will say Symbol not found i assume meaning a Variable Connection has not been declared.

thanx allot

Posted: 12 Apr 2006, 14:52
by Scarlet_Spider
dont know if this will help but here it is none the less http://www.connectionstrings.com/