redi
redi
import java.sql.Connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;
import java.util.logging.Level;
public tr() {
initComponents();
createConnection();
}
/**
* This method is called from within the constructor to initialise 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">
jLabel2.setText("jLabel2");
jLabel4.setText("jLabel4");
jLabel5.setText("jLabel5");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("id");
jLabel3.setText("name");
jLabel6.setText("department");
jLabel7.setText("age");
jButton1.setText("Regiter");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
tr lo=new tr();
lo.createConnection();
}
void createConnection(){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new tr().setVisible(true);
try{
// Load the JDBC drive
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/redi","root","root");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from lab1");
while(rs.next()){
String name=rs.getString("id");
String age=rs.getString("name");
String n=rs.getString("department");
String a=rs.getString("age");
System.out.println(name);
System.out.println(" ");
System.out.println(age);
System.out.println(" ");
System.out.println(n);
System.out.print(" ");
System.out.println(a);
System.out.println("Database Connected Successfully.");
}
} catch(ClassNotFoundException ex){
Logger.getLogger(tr.class.getName()).log(Level.SEVERE, null, ex);
} catch(SQLException ex){
Logger.getLogger(tr.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}