What's new

Closed Mysql database from other pc connection error

Status
Not open for further replies.

vortex78

Honorary Poster
Joined
Apr 14, 2017
Posts
432
Reaction
91
Points
172
Age
26
mga idol help naman po. gusto po kasi ng prof. ko kukuha ng database from other pc. ayos naman po yun connection ng virtual machine ko at host pc. pero hindi po naconnect sa mysql dababase. ano po kaya mali sa connection ko? eto po yun system ko

package MovieTicketBooking;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

public class LOGIN extends javax.swing.JFrame {

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static String DB_URL = "jdbc:mysql://192.168.137.2/mtbadmin";
static final String USER = "root";
static final String PASS = "";
Connection conn = null;
Statement stmt = null;

public LOGIN() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jTextField1 = new javax.swing.JTextField();
login = new javax.swing.JButton();
username = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
password = new javax.swing.JPasswordField();
jLabel4 = new javax.swing.JLabel();

jTextField1.setText("jTextField1");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Log In");

login.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
login.setText("LOG IN");
login.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loginActionPerformed(evt);
}
});

username.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel1.setText("USERNAME");

jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel2.setText("PASSWORD");

password.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/MovieTicketBooking/logo.png"))); // NOI18N

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel4))
.addGroup(layout.createSequentialGroup()
.addGap(76, 76, 76)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(password)
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(131, 131, 131)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(8, 8, 8)
.addComponent(jLabel1)
.addGap(18, 18, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(password, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(78, 78, 78))
);

pack();
}// </editor-fold>

private void loginActionPerformed(java.awt.event.ActionEvent evt) {
try {
Connection myconnection;

myconnection = DriverManager.getConnection(DB_URL, USER, PASS);

try {
String q = "select * from admin where Username=? and Password=?";
PreparedStatement mystatement = myconnection.prepareStatement(q);
mystatement.setString(1, username.getText());
mystatement.setString(2, password.getText());
ResultSet myresult = mystatement.executeQuery();
if (myresult.next()) {
(myglobalclass.uname) = username.getText();
ADMIN obj = new ADMIN();
this.dispose();
obj.setVisible(true);
} else {

String q2 = "select * from employee where Username=? and Password=?";
PreparedStatement mystatement1 = myconnection.prepareStatement(q2);
mystatement1.setString(1, username.getText());
mystatement1.setString(2, password.getText());
ResultSet myresult1 = mystatement1.executeQuery();
if (myresult1.next()) {
(myglobalclass.uname) = username.getText();
EMPLOYEE obj = new EMPLOYEE();
this.dispose();
obj.setVisible(true);
} else {
JOptionPane.showMessageDialog(rootPane, "Wrong username/passsword", "Error", JOptionPane.ERROR_MESSAGE);
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(rootPane, "Error in query due to" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
} finally {
myconnection.close();
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(rootPane, "Error in connection" + e.getMessage());
}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see You do not have permission to view the full content of this post. Log in or register now.
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(LOGIN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(LOGIN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(LOGIN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(LOGIN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new LOGIN().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField jTextField1;
private javax.swing.JButton login;
private javax.swing.JPasswordField password;
private javax.swing.JTextField username;
// End of variables declaration
}
 
This is updated source or outdated o matagal ng source ?? Hmm may mga source kasing dapat ng I re update. , tama naman yung source code mo paps. Pero I try ko irecode tas iconnect ko sa SQL data based ko
 
Status
Not open for further replies.

Similar threads

Back
Top