0
votes

I am using following code to get my java application connected with Sql Server 2008.

//connecting java with a different database management system
import java.sql.*;
import java.util.Scanner;

public class MyDatabase {

    static Connection conn = null;
    static PreparedStatement ps = null;

    public static void main(String [] arg) {
        String urlp1, urlp2, urlp3;
        urlp1 = "jdbc:sqlserver://;";
        urlp2 = "servername=HP-PC" + "\\" + "MYINSTANCE;integratedSecurity=true;";
        String url = urlp1 + urlp2;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        }
        catch (Exception e) {
            System.out.println("Exception 1");
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(url, "sa", "admin");
        }
        catch (Exception ec) {
            System.out.println("Exception 1 Part 2");
            ec.printStackTrace();
        }
        try {
            System.out.println("Enter name:");
            Scanner scan = new Scanner(System.in);
            String name = scan.nextLine();
            System.out.println("Enter age:");
            int age = scan.nextInt();
            String query = "insert into table1(Stuname,Age) values(?,?);";
            ps = conn.prepareStatement(query);
            ps.setString(1, name);
            ps.setInt(2, age);
            conn.commit();
        }
        catch (Exception e1) {
            System.out.println("Exception 2");
            e1.printStackTrace();
        }
        finally {
            try {
                System.out.println("Closed!!!");
                conn.close();
            }
            catch (Exception e2) {
                System.out.println("Exception 3");
                e2.printStackTrace();
            }
        }
    }
}

I have downloaded "sqljdbc4.0.jar" file and using following command in cmd to locate the address of the SQLServerDriver class-:

set classpath=.;D:\Java\jre\bin\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc4.jar

The problem is data is not entered in the table. Neither any error nor any exception is being shown. I've gone through a lot of solutions on the internet but none of them is working. How can I accomplish this??

1
I think the URl is not correct,see this technet.microsoft.com/en-us/library/ms378428.aspx - SpringLearner
What output do you see printed to the console? Are you actually getting a connection? - MarkOfHall

1 Answers

0
votes

I assume you are not getting any connection

 System.out.println("Enter name:");
        Scanner scan=new Scanner(System.in);
        String name=scan.nextLine();
        System.out.println("Enter age:");
        int age=scan.nextInt();
        String query="insert into table1(Stuname,Age) values(?,?);";
        ps=conn.prepareStatement(query);
        ps.setString(1,name);
        ps.setInt(2,age);

       // you have forget to submit your changes to the database. try with following line of code
        ps.execute() or ps.executeQuery()

        conn.commit();