Create new class named javaconnect
import java.sql.*;
import javax.swing.JOptionPane;
public class javaconnect {
Connection conn;
public static Connection ConnecrDB(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql:///clinic","root","");
return conn;
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
_____________________________________________________________________________
REGISTRATION code
import java.sql.*;
import javax.swing.JOptionPane;
public class Signup extends javax.swing.JFrame {
Connection conn;
ResultSet rs;
PreparedStatement state;
public Signup() {
super("Login");
initComponents();
conn=javaconnect.ConnecrDB();
}
private void registerActionPerformed(java.awt.event.ActionEvent evt) {
String pass = jTxtPass.getText();
String compass = jTxtconfirm.getText();
If( pass==compass){
try{
String sql="Insert into Account (Name,Username,Password) values(?,?,?,?,?)";
state=conn.prepareStatement(sql);
state.setString(1, jTxtName.getText());
state.setString(2, jTxtUser.getText());
state.setString(3, jTxtPass.getText());
state.execute();
JOptionPane.showMessageDialog(null, "New Account Created!");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
Else{
JOptionPane.showMessageDialog(null, "Password not match!");
Log in code
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String sql="Select * from doctor where Username=? and Password=?";
try{
state=conn.prepareStatement(sql);
state.setString(1,jTextuser.getText());
state.setString(2,jPassword.getText());
rs=state.executeQuery();
if(rs.next()){
rs.close();
state.close();
}else{
JOptionPane.showMessageDialog(null, "Incorrect Username and Password!");
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}finally{
try{
rs.close();
state.close();
}catch(Exception e){
}
}
}
Search Code
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String sql="Select * from record where ID=?";
try{
state=conn.prepareStatement(sql);
state.setString(1, jTextField1.getText());
rs=state.executeQuery();
if(rs.next()){
String add1=rs.getString("First_Name");
jTextField2.setText(add1);
String add2=rs.getString("Last_Name");
jTextField4.setText(add2);
String add3=rs.getString("Course");
jTextField3.setText(add3);
String add4=rs.getString("Status");
jTextField5.setText(add4);
String add5=rs.getString("Address");
jTextField9.setText(add5);
String add6=rs.getString("Age");
jTextField10.setText(add6);
String add7=rs.getString("Gender");
jTextField11.setText(add7);
rs.close();
state.close();
}else{
JOptionPane.showMessageDialog(null, "Student ID not Found!");
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
Insert code
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String sql="Insert into doctor_data
(ID,LName,FName,Course,Status,Address,Age,Gender,Temperature,Weight,BP,Height,Pulse_Rate
) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
try{
state=conn.prepareStatement(sql);
state.setString(1, jTextField1.getText());
state.setString(2, jTextField2.getText());
state.setString(3, jTextField4.getText());
state.setString(4, jTextField3.getText());
state.setString(5, jTextField5.getText());
state.setString(6, jTextField9.getText());
state.setString(7, jTextField10.getText());
state.setString(8, jTextField11.getText());
state.setString(9, jTextField12.getText());
state.setString(10, jTextField13.getText());
state.setString(11, jTextField14.getText());
state.setString(12, jTextField15.getText());
state.setString(13, jTextField16.getText());
state.execute();
JOptionPane.showMessageDialog(null, "Send to doctor");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
Show data to table
public void showdata (){
String sql = "Select * from doctor_data";
try{
state=conn.prepareStatement(sql);
rs = state.executeQuery();
jTable2.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
}
Code to click selected Row from table to Textfields
private void jTable1MouseClicked(java.awt.event.MouseEvent
evt) {
// TODO add your handling code here:
int row=jTable1.getSelectedRow();
String tc= jTable1.getModel().getValueAt(row,
0).toString();
try{
String sql= "Select * from newbook where
Book_ID='"+tc+"'";
state=conn.prepareStatement(sql);
rs = state.executeQuery();
if(rs.next()){
int id=rs.getInt("Book_ID");
String name=rs.getString("Name");
String edition=rs.getString("Edition");
String publisher=rs.getString("Publisher");
String price=rs.getString("Price");
String pages=rs.getString("Pages");
jTextField1.setText(""+id);
jTextField2.setText(name);
jComboBox1.setSelectedItem(edition);
jTextField3.setText(publisher);
jTextField4.setText(price);
jTextField5.setText(pages);
}
state.close();
rs.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
UPDATE CODE
public void update(){
try{
Connection connec = null;
PreparedStatement state = null;
ResultSet rs = null;
String Med_ID = jTextField1.getText().toString();
String Med_Name = jTextField2.getText().toString();
String Quantity = jTextField5.getText().toString();
String Company = jTextField3.getText().toString();
String Price = jTextField4.getText().toString();
Class.forName("com.mysql.jdbc.Driver");
connec =
DriverManager.getConnection("jdbc:mysql:///clinic","root","");
String sql = "Update medicines set Med_Name =
'"+jTextField2.getText()+"',Quantity =
'"+jTextField5.getText()+"',Company =
'"+jTextField3.getText()+"',Price = '"+jTextField4.getText()+"'WHERE
Med_ID = '"+jTextField1.getText()+"'";
state = connec.prepareStatement(sql);
state.execute();
showdata();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
USING JTABBEDPANE AND PANEL
Steps:
1.Add TabbedPane to the frame rename its variable
2. Add/drag panel into the TabbedPane
Connect the button to TabbedPane
Sample code:
jtransactions.setSelectedIndex(0);
note… index starts at 0 meaning first tab is index 0, 1,2…..
jtransactions is the id of tabbedpane
code to center the frame on screen
public scrllFrame() {
initComponents();
centerFrame();
initIcons();
}
private void centerFrame() { // call this to initcomponents()
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
int x = (screenSize.width - this.getWidth()) / 2;
int y = (screenSize.height - this.getHeight()) / 2;
this.setLocation(x, y);
}
Print code from table
private void jButton2ActionPerformed(java.awt.event.ActionEvent
evt) {
try{
MessageFormat header = new MessageFormat("Page
Header");
MessageFormat footer = new
MessageFormat("Page{1,number,integer}");
jTable2.print(JTable.PrintMode.FIT_WIDTH, header, footer);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
Note:
When building your system, you can refer to these codes for
several instances. In addition, you will conduct an online search for
your project.