Click here to Skip to main content
15,899,679 members
Home / Discussions / Java
   

Java

 
GeneralRe: java media player with Java Media Frmework Pin
Md Shariful Islam Saful26-Jun-13 10:00
Md Shariful Islam Saful26-Jun-13 10:00 
AnswerRe: java media player with Java Media Frmework Pin
Shubhashish_Mandal3-Jul-13 19:59
professionalShubhashish_Mandal3-Jul-13 19:59 
AnswerRe: java media player with Java Media Frmework Pin
MarlBermudoNights3-Jul-13 22:22
professionalMarlBermudoNights3-Jul-13 22:22 
QuestionConnection to SQL using JDBC in Java Pin
chdboy25-Jun-13 23:23
chdboy25-Jun-13 23:23 
AnswerRe: Connection to SQL using JDBC in Java Pin
thanh_bkhn28-Jun-13 1:12
professionalthanh_bkhn28-Jun-13 1:12 
GeneralRe: Connection to SQL using JDBC in Java Pin
chdboy28-Jun-13 4:06
chdboy28-Jun-13 4:06 
GeneralRe: Connection to SQL using JDBC in Java Pin
Richard MacCutchan28-Jun-13 4:12
mveRichard MacCutchan28-Jun-13 4:12 
GeneralRe: Connection to SQL using JDBC in Java Pin
chdboy28-Jun-13 5:06
chdboy28-Jun-13 5:06 
Java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;

public abstract class ConnectionDB 
{	
	
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{	
		
		try {						 
			insertRecordIntoDbUserTable();
			selectfromdb();
		} 
		catch (SQLException e)
		{
 
			System.out.println(e.getMessage());
 
		}
}
	
		
	private static void insertRecordIntoDbUserTable() throws SQLException
	{
		Connection con = null;
		Statement statement = null;
		String insertTableSQL = "INSERT INTO LoginDetails" + "(Username, Password) " + "VALUES" + "('user1','user1')";
		try 
		{
			con = DBConnectionstring();
			statement = con.createStatement(); 
			System.out.println(insertTableSQL);			
			statement.executeUpdate(insertTableSQL);			
			
			JOptionPane.showMessageDialog(null,"Your Data has been Inserted","Data Inserted",JOptionPane.WARNING_MESSAGE);
		} 
		catch (SQLException e)
		{
 
			System.out.println(e.getMessage()); 
		} 
		finally 
		{
 
			if (statement != null)
			{
				statement.close();
				
				JOptionPane.showMessageDialog(null,"Statement is closed","Statement",JOptionPane.WARNING_MESSAGE);
			}
 
			if (con != null)
			{
				con.close();
				
				JOptionPane.showMessageDialog(null,"Connection is closed!","Connection",JOptionPane.WARNING_MESSAGE);
			}
 
		}
		
		
	}
	
	private static void selectfromdb() throws SQLException
	{		
	    Statement stmt = DBConnectionstring().createStatement();
		ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails");
		while (rs.next())
		{
			  String lastName = rs.getString("Username");
			  String Pass = rs.getString("Password");
			  System.out.println(lastName + "" + Pass + "\n");
			 
			}		
	}
	
	 static Connection DBConnectionstring()
	{		
		Connection con = null;
		String conUrl = "jdbc:sqlserver://localhost:1433; databaseName=paytest; user=PC; password=; integratedSecurity=true;";
		
		try 
		{
				 con = DriverManager.getConnection(conUrl);
				
				 JOptionPane.showMessageDialog(null,"Connection is open!","Connection",JOptionPane.WARNING_MESSAGE);
				 
				 }
		catch (SQLException e)
		{
			
			e.printStackTrace();
		}		
		return con;
		
	}
	

	}


The only difference is that this is an Abstract class.


And this is the code I'm using now with copy for connection string to the above code .



Java
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.awt.Container;
import javax.swing.JOptionPane;
import java.awt.Frame;
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SpringLayout;



public class SpringSample extends Frame implements ActionListener

{
	
	 JLabel F_namelbl = new JLabel("Username: ");
	    JLabel L_namelbl = new JLabel("Password: ");
	    JTextField tf_Fname = new JTextField(15);
	    JPasswordField tf_Lname = new JPasswordField(15);
	    JLabel Lbl_HW = new JLabel("< Login Area >");
	    JButton Btn = new JButton("Login");
	    JButton btn_cancel = new JButton("Cancel");
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	SpringSample()
	{
		
	    
		 JFrame frame = new JFrame("Login Area!");
		    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		    Container contentPane = frame.getContentPane();
		    JPanel p = new JPanel(new SpringLayout());
		    SpringLayout layout = (new SpringLayout());
		    contentPane.setLayout(layout);		   
		    JPanel p1=new JPanel();    
		   
		     
		    //--------------------------------------------------------------------------------------------------------------------
		    
		    //---------------------------------------------------------------------------------------------------------------------
		    /*Adding Components to the ContentPane */
		    frame.add(p1);
		    contentPane.add(F_namelbl);
		    contentPane.add(L_namelbl);
		    contentPane.add(tf_Fname);
		    contentPane.add(tf_Lname);
		    contentPane.add(Lbl_HW);
		    contentPane.add(Btn);
		    contentPane.add(btn_cancel);
		    	    
		      //--------------------------------------------------------------------------------------------------------------------
		    /*Lay out Components according to the position and coordinates on the SpringLayout*/
		    
		    layout.putConstraint(SpringLayout.WEST, Lbl_HW, 140, SpringLayout.WEST, contentPane);
		    layout.putConstraint(SpringLayout.NORTH, Lbl_HW, 0, SpringLayout.NORTH, contentPane);    
		    layout.putConstraint(SpringLayout.WEST, Lbl_HW, 140, SpringLayout.WEST, contentPane);
		    layout.putConstraint(SpringLayout.NORTH, Lbl_HW, 0, SpringLayout.NORTH, contentPane);
		    layout.putConstraint(SpringLayout.WEST, F_namelbl, 30, SpringLayout.WEST, contentPane);
		    layout.putConstraint(SpringLayout.NORTH, F_namelbl, 25, SpringLayout.NORTH, contentPane);
		    layout.putConstraint(SpringLayout.WEST, L_namelbl, 30, SpringLayout.WEST, contentPane);
		    layout.putConstraint(SpringLayout.NORTH, L_namelbl, 50, SpringLayout.NORTH, contentPane);
		    layout.putConstraint(SpringLayout.NORTH, tf_Fname, 25, SpringLayout.NORTH, contentPane);
		    layout.putConstraint(SpringLayout.WEST, tf_Fname, 15, SpringLayout.EAST, F_namelbl);
		    layout.putConstraint(SpringLayout.NORTH, tf_Lname, 50, SpringLayout.NORTH, contentPane);
		    layout.putConstraint(SpringLayout.WEST, tf_Lname, 15, SpringLayout.EAST, F_namelbl);
		    layout.putConstraint(SpringLayout.NORTH, Btn, 80, SpringLayout.NORTH, contentPane);
		    layout.putConstraint(SpringLayout.WEST, Btn, 110, SpringLayout.WEST, contentPane);
		    layout.putConstraint(SpringLayout.NORTH, btn_cancel, 80, SpringLayout.NORTH, p1);
		    layout.putConstraint(SpringLayout.WEST, btn_cancel, 200, SpringLayout.WEST, p1);	    
		    
		    //---------------------------------------------------------------------------------------------------------------------
		    /*Adding Action Listener/Action*/
		    btn_cancel.addActionListener(this);
		    Btn.addActionListener(this);
		    
		    //------------------------------------------------------------------------------------------------------------------------
		    /*Setting up JFrame*/
		    
		    frame.pack();
		    frame.setSize(350, 200);
		    frame.setVisible(true);	
	}
  public static void main(String args[]) 
  {	 
	  
    new SpringSample();
    Connectionstring();
    
    
    try {						 
		insertRecordIntoDbUserTable();
		selectfromdb();
	} 
	catch (SQLException e)
	{

		System.out.println(e.getMessage());

	}  
	  
  }
  //connection string method
  static Connection Connectionstring()
 	{		
 		Connection con = null;
 		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
 		String conUrl = "jdbc:sqlserver://localhost:1433; databaseName=paytest; user=PC; password=; integratedSecurity=true;";
 		
 		try 
 		{
 				 con = DriverManager.getConnection(conUrl); 				 
 				 JOptionPane.showMessageDialog(null,"Connection is open!","Connection",JOptionPane.WARNING_MESSAGE);
 				 
 				 }
 		catch (SQLException e)
 		{
 			
 			e.printStackTrace();
 		}		
 		return con;
 		
 	}
  
  private static void insertRecordIntoDbUserTable() throws SQLException
	{
		Connection con = null;
		Statement statement = null;
		String insertTableSQL = "INSERT INTO LoginDetails" + "(Username, Password) " + "VALUES" + "('user1','user1')";
		try 
		{
			con = Connectionstring();
			statement = con.createStatement(); 
			System.out.println(insertTableSQL);			
			statement.executeUpdate(insertTableSQL);			
			
			JOptionPane.showMessageDialog(null,"Your Data has been Inserted","Data Inserted",JOptionPane.WARNING_MESSAGE);
		} 
		catch (SQLException e)
		{

			System.out.println(e.getMessage()); 
		} 
		finally 
		{

			if (statement != null)
			{
				statement.close();
				
				JOptionPane.showMessageDialog(null,"Statement is closed","Statement",JOptionPane.WARNING_MESSAGE);
			}

			if (con != null)
			{
				con.close();
				
				JOptionPane.showMessageDialog(null,"Connection is closed!","Connection",JOptionPane.WARNING_MESSAGE);
			}

		}
		
		
	}
 
  private static void selectfromdb() throws SQLException
	{		
	    Statement stmt = Connectionstring().createStatement();
		ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails");
		while (rs.next())
		{
			  String lastName = rs.getString("Username");
			  String Pass = rs.getString("Password");
			  System.out.println(lastName + "" + Pass + "\n");
			 
			}		
	}
  
  
  

 public void actionPerformed(ActionEvent e) 
{
	 
	if(e.getSource() == btn_cancel)
	{
		JOptionPane.showMessageDialog(null,"You have clicked on Cancel","button cancel",JOptionPane.WARNING_MESSAGE);
	}
	else if(e.getSource() == Btn)
	{
		JOptionPane.showMessageDialog(null,"You have clicked login","button login",JOptionPane.WARNING_MESSAGE);		
		
	}
	
	

}
}

GeneralRe: Connection to SQL using JDBC in Java Pin
Richard MacCutchan28-Jun-13 5:14
mveRichard MacCutchan28-Jun-13 5:14 
GeneralRe: Connection to SQL using JDBC in Java Pin
chdboy28-Jun-13 5:17
chdboy28-Jun-13 5:17 
GeneralRe: Connection to SQL using JDBC in Java Pin
Richard MacCutchan28-Jun-13 6:00
mveRichard MacCutchan28-Jun-13 6:00 
GeneralRe: Connection to SQL using JDBC in Java Pin
chdboy28-Jun-13 6:16
chdboy28-Jun-13 6:16 
GeneralRe: Connection to SQL using JDBC in Java Pin
Richard MacCutchan28-Jun-13 6:45
mveRichard MacCutchan28-Jun-13 6:45 
GeneralRe: Connection to SQL using JDBC in Java Pin
chdboy28-Jun-13 6:46
chdboy28-Jun-13 6:46 
QuestionHow to implement a Struts project with Spring together? Pin
JasminHan25-Jun-13 18:22
professionalJasminHan25-Jun-13 18:22 
AnswerRe: How to implement a Struts project with Spring together? Pin
Arindam Mondal from India28-Jun-13 6:56
Arindam Mondal from India28-Jun-13 6:56 
GeneralRe: How to implement a Struts project with Spring together? Pin
JasminHan30-Jun-13 22:03
professionalJasminHan30-Jun-13 22:03 
GeneralRe: How to implement a Struts project with Spring together? Pin
Arindam Mondal from India1-Jul-13 3:54
Arindam Mondal from India1-Jul-13 3:54 
Questionreading and writing using smartcard?? Pin
Sairam3925-Jun-13 0:36
Sairam3925-Jun-13 0:36 
QuestionBeginner in Java. I want to test my knowledge Pin
Adrian Manuel Tan Cleofe22-Jun-13 16:51
Adrian Manuel Tan Cleofe22-Jun-13 16:51 
AnswerRe: Beginner in Java. I want to test my knowledge Pin
Richard MacCutchan22-Jun-13 23:51
mveRichard MacCutchan22-Jun-13 23:51 
AnswerRe: Beginner in Java. I want to test my knowledge Pin
JasminHan25-Jun-13 18:43
professionalJasminHan25-Jun-13 18:43 
AnswerRe: Beginner in Java. I want to test my knowledge Pin
MarlBermudoNights3-Jul-13 22:31
professionalMarlBermudoNights3-Jul-13 22:31 
QuestionJava application path in Eclipse juno show wrong neet to set correct Pin
Balmukund Dewangan21-Jun-13 4:21
Balmukund Dewangan21-Jun-13 4:21 
AnswerRe: Java application path in Eclipse juno show wrong neet to set correct Pin
Richard MacCutchan21-Jun-13 5:25
mveRichard MacCutchan21-Jun-13 5:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.