자바

 

class SQLInsertTest

package java19;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Properties;



public class SQLInsertTest {	
	public static void main(String[] args) throws Exception {
		FileInputStream fis =new FileInputStream("db.prop");
		Properties prop =new Properties();//db.prop 내용을 읽어서 key, value 구조로 변환
		prop.load(fis); // db.prop 내용을 읽어서 key, value 구조로 변환
		String driver=prop.getProperty("driver"); // getProperty("변수명")
		String url =prop.getProperty("url");
		String id=prop.getProperty("id");
		String password=prop.getProperty("password");
		
		System.out.println("driver :" +driver);
		
/*		String url ="jdbc:mysql://localhost:3305/java";
		String id="java";
		String password="1111";
		
		*/
		Connection conn=null; 
		Statement stmt=null;
		PreparedStatement pstmt=null;
		try{
			//Class.forName("com.mysql.jdbc.Driver");
			Class.forName(driver);
			System.out.println("드라이버가 존재합니다.");
			
			conn=DriverManager.getConnection(url, id, password);
			System.out.println("mysql 연결 성공");
			
			String title="C#";
			String publisher="C#출판사";
			int year=2017;
			int price=45000;
			
/*			String sql ="insert into books (title, publisher, year, price )"
					+ " values ('java', '자바출판사', 2017, 40000 )";
			String sql2 =String.format("insert into books (title, publisher, year, price )"
					+ "values ('%s', '%s', %d, %d )" , title ,  publisher , year, price);
	
	*			
			stmt=conn.createStatement();
			//executeUpdate() 추가, 수정, 삭제
			// executeQuery() select 
			// execute()  추가
			stmt.execute(sql2);

	*/				

//아직 완성된 sql 문장이 아님 ,값이 입력될 위치에 ? 삽입
			String sql ="insert into books (title, publisher, year, price) "
					+ " values ( ? , ? , ?, ?)";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, title);
			pstmt.setString(2, publisher);
			pstmt.setInt(3, year);
			pstmt.setInt(4, price);
			pstmt.execute();
			System.out.println("추가되었습니다");
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(stmt!=null)stmt.close();
				if(pstmt!=null)pstmt.close();
				if(conn!=null)conn.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	
		
		
	}
	
	
}




 

driver :com.mysql.jdbc.Driver
드라이버가 존재합니다.
mysql 연결 성공
추가되었습니다
 

db.prop

url=jdbc:mysql://localhost:3305/java
driver=com.mysql.jdbc.Driver
id=java
password=1111

 

 

 

 

 

 

 

class LoginTest

 

package java19;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.JPasswordField;

public class LoginTest extends JFrame {

	private JPanel contentPane;
	private JLabel lblResult;
	private JTextField userid;
	private JPasswordField pwd;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginTest frame = new LoginTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginTest() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 484, 387);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("아이디");
		lblNewLabel.setBounds(48, 65, 57, 15);
		contentPane.add(lblNewLabel);
		
		userid = new JTextField();
		userid.setBounds(155, 62, 194, 21);
		contentPane.add(userid);
		userid.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("비밀번호");
		lblNewLabel_1.setBounds(48, 126, 57, 15);
		contentPane.add(lblNewLabel_1);
		
		JButton btnLogin = new JButton("로그인");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				private JLabel lblResult;
//				private JTextField userid;
//				private JTextField pwd;

				//사용자가 입력한 아이디, 비번
				String uid=userid.getText();
				String pw =String.valueOf(pwd.getPassword());
				
				
				login(uid, pw);
				
			}
		});
		btnLogin.setBounds(140, 186, 144, 23);
		contentPane.add(btnLogin);
		
		lblResult = new JLabel("");
		lblResult.setBounds(48, 247, 301, 32);
		contentPane.add(lblResult);
		
		pwd = new JPasswordField();
		pwd.setBounds(155, 123, 194, 21);
		contentPane.add(pwd);
	}
	
	
	
	private void login(String mid, String mPassword){
		FileInputStream fis=null;
		Connection conn=null;
		PreparedStatement pstmt =null;
		ResultSet rs=null;
		try{
			fis =new FileInputStream("db.prop");
			Properties prop =new Properties();
			prop.load(fis);
			String id =prop.getProperty("id");
			String password=prop.getProperty("password");
			String driver=prop.getProperty("driver");
			String url=prop.getProperty("url");
			
/*		 	userid varchar(50) not null primary key,
		 	pwd varchar(50) not null,
		   name varchar(50) not null*/
			
			Class.forName(driver);
			System.out.println("드리아버 존재");
			
			conn=DriverManager.getConnection(url, id, password);
			
			String sql ="select * from member where userid=? and pwd=? ";
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, mid);
			pstmt.setString(2, mPassword);
			
			rs=pstmt.executeQuery();
			
			String resultUserId="";
			String resultPwd="";
			String resultName="";
			
			if(rs.next()){	
				resultUserId=rs.getString("userid");
				resultPwd= rs.getString("pwd");
				resultName=rs.getString("name");
				lblResult.setText(resultName+"님 로그인 되었습니다. \n"
						+ " " + resultUserId + " , " +resultPwd 
						);
			}else{
				lblResult.setText("아이디 또는 패스워드가 틀립니다.");		
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null)rs.close();
				if(pstmt!=null)pstmt.close();
				if(conn!=null)conn.close();
				if(fis!=null)fis.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	
	
}






 

 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

가장 높은 곳에 뜻을 두어라. -모택동

댓글 ( 4)

댓글 남기기

작성