简体   繁体   中英

JDBC program to pull from database

So I have fully programmed a JDBC file which pulls from a MySQL database. Now I would like to use my file for a "2008 SQL SERVER R2". For my JDBC program I currently have it set up as:

public class JDBCPullFromTable {
 //JDBC driver name and database URL
 static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
 static final String DB_URL = "jdbc:mysql://localhost:3306/MyDB";
 static final String USER = "root";
 static final String PASS = "pw";
.....
...
..
.
  Class.forName("com.mysql.jdbc.Driver");

 //Open a connection
 //System.out.println("Connecting to a selected database...");
 conn = DriverManager.getConnection(DB_URL, USER, PASS);

From this point how can I get it to connect to a 2008 SQL SERVER R2 Instead? Access this database using Microsoft SQL Server Management Studio. I'm assuming I will download the JDBC driver, but what about the username and password? Will I use the same which is used to sign into the studio? Thanks

Currently what I have...

 static final String JDBC_DRIVER = "**NOWIDEAWHATTHISIS**";  
 static final String DB_URL = "jdbc:sqlserver://localhost:1433/NEdatabasename";

 //Database credentials
 static final String USER = "neuser";
 static final String PASS = "nepass";

I believe that what you are looking for is:

com.microsoft.sqlserver.jdbc.SQLServerDriver

To connect to the SQL server you can use

     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
     Connection conn = DriverManager.getConnection(DB_URL,
              USER, PASS);

And then do what you please with the connection.

Firstly you need to download the jdbc driver for sql serveur 2008. Secondly username and password are the same as those you use to connect to the database. So it will be like this:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
static final String DB_URL = "jdbc:sqlserver://localhost:1433/NEdatabasename";

//Database credentials
static final String USER = "neuser";//Username of the db
static final String PASS = "nepass";//password of the db
Connection conn = DriverManager.getConnection(DB_URL,USER, PASS);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM