Click here to Skip to main content
14,384,421 members
Rate this:
Please Sign up or sign in to vote.
I am using windows authenication in sql server 2012.

I want to connect java to sql.

I have following code (just for example AND Some code is missing as copied from netbeans you know)


public class app extends javax.swing.JFrame {

public app() {
    initComponents();
}



@SuppressWarnings("unchecked")


private void UserNameActionPerformed(java.awt.event.ActionEvent evt) {

          String username = UserName.getText();
}

private void PasswordActionPerformed(java.awt.event.ActionEvent evt) {

    String password = Password.getText();

}

private void LoginActionPerformed(java.awt.event.ActionEvent evt) {


}


public static void main(String args[]) {


    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new app().setVisible(true);
        }
    });




}

// Variables declaration - do not modify
private javax.swing.JToggleButton Login;
private javax.swing.JPasswordField Password;
private javax.swing.JTextField UserName;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
// End of variables declaration
}





/////////////AND DATABASE QURIES/////////////////////


Create database ali;

use ali;


create table Login_System (

		Username VARCHAR(50),
		password VARCHAR(90)

)


SELECT * FROM Login_System;


What I have tried:

String userName ="username";
String password ="password";

String url ="jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB";

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(apps.class.getName()).log(Level.SEVERE, null, ex);
        }
Connection conn = DriverManager.getConnection(url, userName, password);
Posted
Updated 19-Jun-16 4:51am
v5
Comments
CHill60 21-Jun-16 6:42am
   
So what is your question?
Stack Holder 23-Jun-16 8:01am
   
Problem with connection...
Also tried the following code:

try
{
String url = "jdbc:sqlserver://localhost:1433//MALIKUSMANNAWAZ;databaseName=ali";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url);
System.out.println("connection created");
Statement st=conn.createStatement();
String sql="select * from Login_System";
ResultSet rs=st.executeQuery(sql);
while(rs.next())
{
System.out.println("Name: "+rs.getString(1));
//System.out.println("Address : "+rs.getString(2));
}
if(st!=null)
st.close();
if(conn!=null)
conn.close();
}
catch(SQLException sqle)
{
System.out.println("Sql exception "+sqle);
}

but error:

Sql exception com.microsoft.sqlserver.jdbc.SQLServerException: The port number 1433//MALIKUSMANNAWAZ is not valid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100