MS-SQL database connection code and Steps
step1:Create your instance using the sqlexpre.exe file select the required settings during the setup.
step2. after creating the instance open the sql server conf manager
step3. enable the tcp/ip and do the required settings for the port no (1433) and local host ip address (127.0.0.1)
step4.start the created instance.
step5.open the cmd prompt
step6. type c:\>sqlcmd
step7. c:\>sp_databasesc:\>go (Note: this command will show u the default databases)
step8. c:\>create database databasename c:\>go
step9.c:\>exit
ster10.Add the requires version of .jar file means driver into the libraries folder in your project.
step11. open the services window in netbeans
step12.RClick on the database option select the new connection option.
step13.load the appropriate driver in the basic settings tab. (your .jar file of the driver)
step14.give the required values in the shows fields.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /* JAVA AND MSSQL DATABASE CONNECTIVITY */ import java.sql.* class JavaSqlServer { public static void main(String rk[]) { try { //Load and register SQL Server driver Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //Establish a connection Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://serverjag:1433","sa",""); //Create a Statement object Statement stmt = conn.createStatement(); //Create a ResultSet object, execute the query and return a resultset ResultSet rset = stmt.executeQuery("SELECT * FROM Data"); int i=0; while (rset.next()) { System.out.println(rs.getString(1)); } //Close the ResultSet and Statement rset.close(); stmt.close(); //Close the database connection conn.close(); } catch(Exception e) { System.out.println("ERROR : "+e); } } } |