Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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)


Java
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/////////////////////


SQL
Create database ali;

use ali;


create table Login_System (

		Username VARCHAR(50),
		password VARCHAR(90)

)


SELECT * FROM Login_System;


What I have tried:

SQL
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 3: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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900