Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Lỗi kết nối MySQL trong Java

    Hiện tại em mới làm quen với Java nhưng được giao một bài toán về quản lý nhân sự. Bài toán cụ thể là, tạo một chương trình có khả năng thêm, xóa, tìm kiếm thông tin của các đối tượng được lưu trong database. Các đối tượng có các thông tin (tên, mã sinh viên, tuổi). Em có trình bày đoạn code như thế này, nhưng không thể liên kết được với database, các anh có thể giải thích cho em các nguyên nhân để em có thể khắc phục không ạ. Các anh nhìn qua code, có góp ý gì để em sửa ạ. Em mới tìm hiểu nên code nó hơi vụn vặt và có thể sai ở điểm nào đó, mong các anh cho ý kiến ạ. Em xin cảm ơn.
    Mã:
    package com.testlab;
    
    import java.util.*;
    import java.sql.*;
    import javax.print.attribute.standard.PresentationDirection;
    
    
    public class Employee {
    	
    	private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
    	private static final String DB_CONNECTION = "jdbc:mysql://127.0.0.1:3306/employee";
    	private static final String DB_USER = "root";
    	private static final String DB_PASS = "123";
    	
    	
    	public static void main(String[] argv)
    	{
    		int urChoice,urOld,urId = 0;
    		 
    		
    		
    		System.out.println("_Welcome to MyWorld_");
    		System.out.print("-----------------------------------------------------------------
    ");
    		System.out.print("--           			(0) : Thoat chuong trinh                 	 --
    ");
    		System.out.print("--           			(1) : Them mot user                     	 --
    ");
    		System.out.print("--           			(2) : Xoa user					 --
    ");
    		System.out.print("--           			(3) : Tim kiem				 	 --
    ");
    		System.out.print("-----------------------------------------------------------------
    ");
    		
    		// nhap phim chuc nang
    		
    		Scanner choice=new Scanner(System.in);
    		String eName = null;
    		do{
    			System.out.print("Chuc nang : ");
    			urChoice = choice.nextInt();
    			switch(urChoice)
    			{
    			case 0 :
    				System.out.println("System stopped");
    				System.exit(0);
    			case 1 :
    				System.out.println("Name : ");
    				eName = choice.nextLine();
    				System.out.println("Old : ");
    				urOld = choice.nextInt();
    				System.out.println("ID : ");
    				urId =eId.nextInt();
    				AddUser(eName, urOld, urId);
    				break;
    			case 2 :
    			
    				System.out.println("Id : ");
    				urId = choice.nextInt();
    				System.out.println("Name : ");
    				eName = choice.nextLine();
    				System.out.println("Old : ");
    				urOld = choice.nextInt();
    				DeleteUser(urId, eName, urOld);
    				break;
    				
    			case 3 :
    				System.out.println("Id : ");
    				urId = choice.nextInt();
    				System.out.println("Name : ");
    				eName = choice.nextLine();
    				System.out.println("Old : ");
    				urOld = choice.nextInt();
    				Search(urId, eName, urOld);
    				break;
    				default: 
    					System.out.println("You Entered wrong menuOption ! Try again .
    ");
    					break;
    			}
    			
    		}while(urChoice !=0);
    		
    	}
    	
    	/* Add */
    	public static void AddUser(String name,int old, int id){
    		Connection con = getConnect();
    		PreparedStatement stmt = null;
    		String query = " INSERT INTO Employee (name,old) VALUES (?,?)";
    		try{
    			stmt = con.prepareStatement(query);
    			stmt.setString(1, name);
    			stmt.setInt(2, old);
    			stmt.executeUpdate();
    			System.out.println("OK!");
    		}catch(Exception e)
    		{
    			e.getStackTrace();
    		}
    		
    	}
    	
    	/* Search */
    	public static void Search(int id,String name,int old){
    		Connection con = getConnect();
    		PreparedStatement stmt = null;
    		String query = "Select * from Employee where id=? or name=? or old=? ";
    		try {
    			stmt = con.prepareStatement(query);
    			stmt.setInt(1, id);
    			stmt.setString(2, name);
    			stmt.setInt(3, old);
    			ResultSet res = stmt.executeQuery();
    			while(res.next()){
    				System.out.println("____________________________");
    				System.out.println("ID : "+res.getInt("id"));
    				System.out.println("Name : "+res.getString("name"));
    				System.out.println("Old : "+res.getInt("old"));
    				System.out.println("____________________________");
    				
    			}
    		}catch(Exception e){
    			e.getStackTrace();
    		}
    		
    	}
    	
    	/*Delete*/
    	
    	public static void DeleteUser(int id,String name,int old)
    	{
    		Connection con = getConnect();
    		PreparedStatement stmt = null ;
    		String query = "DELETE FROM EMPLOYEE WHERE id=? or name=? or old = ?";
    		try{
    			stmt = con.prepareStatement(query);
    			stmt.setInt(1, id);
    			stmt.setString(2, name);
    			stmt.setInt(3,old);
    			stmt.executeUpdate();
    			
    		}
    		catch(Exception e)
    		{
    			e.getMessage();
    		}
    	}
    	
    	public static Connection getConnect(){
    		
    		Connection con = null ;
    		try {
    			
    			Class.forName(DB_DRIVER);
    			
    		} catch (Exception e) {
    			e.getMessage();
    			
    		}
    		
    		try {
    			con = DriverManager.getConnection(DB_CONNECTION,DB_USER,DB_PASS);
    			return con;
    		} catch (Exception e) {
    			// TODO: handle exception
    			e.getStackTrace();
    		}
    		return con;
    		
    		
    	}
    }

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bạn kiểm tra giúp mình xem có thư viện JDBC driver for MySQL chưa ?

    http://dev.mysql.com/downloads/connector/j/5.1.html

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •