Credo fosse un problema di driver e di impostazione del server MySQL...
Ora, dopo 123213123 smanettamenti, son riuscito ad accedere al db e a creare anche la finestra di login solo che non riesco ad interrogare il db per matchare user/passwd dell'utente con quelli presenti nel db...
In parole povere, compare la finestra di login ma non accade nulla quando provo a loggare indipendentemente da quello che inserisco nei campi...
dove sbaglio? Grazie
Qui lo screen del mio db!
https://s14.postimg.org/b16xus8rl/Mysql_demo.png
codice:
import javax.swing.*; //for creating frames
import java.awt.event.*; //for action listeners
import java.sql.*;//for db & queries
public class Login {
// statement variables
Connection con;
Statement st;
ResultSet rs;
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/demo";
static final String USER = "root";
static final String PASSWD = "root";
// creating frame
JFrame f = new JFrame("User Login");
JLabel l = new JLabel("Username");
JLabel l1= new JLabel ("Password");
JTextField t = new JTextField(10);
JTextField t1 = new JTextField(10);
JButton b = new JButton("Login");
public Login()
{
connect();
frame();
}
public void connect()
{
Connection con = null;//create object of Connection and define it null
try //try block
{
//STEP 2: Register JDBC driver
Class.forName(JDBC_DRIVER);
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
con = DriverManager.getConnection(DB_URL, USER, PASSWD);
//print on console
System.out.println("Connected database successfully...");
}
catch(SQLException se) //catch block
{
//Handle errors for JDBC
se.printStackTrace();
}
catch(Exception e) //catch block
{
//Handle errors for Class.forName
e.printStackTrace();
}
finally //finally block
{
//finally block used to close resources
try //try block
{
if(con!=null)//condition
con.close(); //close connection
}
catch(SQLException se)//Handle errors
{
se.printStackTrace();
}//end finally try
}//end try
}
public void frame()
{
f.setSize(500,150);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
JPanel p = new JPanel();
p.add(l);
p.add(t);
p.add(l1);
p.add(t1);
p.add(b);
f.add(p);
b.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e)
{
try
{
String user = t.getText().trim();
String passwd = t1.getText().trim();
String sql = "select username, password from demo.loginaccount where username = '"+user+"'and password = '"+passwd+"'";
rs = st.executeQuery(sql);
int count = 0;
while(rs.next())
{
count = count + 1;
}
if(count == 1)
{
JOptionPane.showMessageDialog(null, "User trovato!");
}
else if(count > 1)
{
JOptionPane.showMessageDialog(null, "Doppio User, Accesso non concesso");
}
else
{
JOptionPane.showMessageDialog(null, "User non trovato!");
}
}
catch(Exception ex)
{
}
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Login();
}
}