Vấn đề lỗi java.lang.NullPointerException khi dùng jbdc kết nối MySQL

nguyenbhb2

New Member
6/5/15
3
0
0
28
Mình có code server làm nhiệm vụ login khi client nhập username và password....nhưng kết nối được database rồi mà cứ báo lỗi java.lang.NullPointerException ở dòng Statement st= con.createStatement();
Code SERVER:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.i:confused:utputStreamWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.*;


public class test {
public static Connection con;;
public static void main(String[] args) throws SQLException, IOException {
ServerSocket server= new ServerSocket(1111);
while(true)
{
Socket conn= server.accept();
Writer gui = new OutputStreamWriter(conn.getOutputStream());
gui.write("Username:"+"\n");
gui.flush();
/*******************************************************/
InputStreamReader in = new InputStreamReader(conn.getInputStream());
StringBuffer username = new StringBuffer();
int c;
while ((c= in.read())!= -1){
if (c== 10) break;
username.append((char)c);
}
gui.write("Password:"+"\n");
gui.flush();
StringBuffer password = new StringBuffer();
while ((c= in.read())!= -1){
if (c== 10) break;
password.append((char)c);
}
DBConnect("test", "", "");
System.out.println("Database is connected");
Statement st= con.createStatement();
String query="Select * from user where user='"+username +"'and pass='"+password+"'";
ResultSet re= st.executeQuery(query);
if(re.next())
{
System.out.println("Login thanh cong");
}else{
System.out.println("Login that bai");
}
conn.close();
}
}
public static void DBConnect(String db, String user, String pass) throws SQLException{
String driver="com.mysql.jdbc.Driver";
String dburl="jdbc:mysql://localhost:3306//"+db+"?user="+user+"&password="+pass;
try {
Class.forName(driver);
con=DriverManager.getConnection(dburl);
} catch (Exception e) {
// TODO: handle exception
}
}
}
Code CLIENT:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.i:confused:utputStreamWriter;
import java.io.PrintStream;
import java.io.Writer;
import java.net.Socket;
import java.net.UnknownHostException;


public class Client {
public static void main(String[] args) throws IOException {
int port=1111;
String host="localhost";
Socket sock =new Socket(host,port);
Writer gui = new OutputStreamWriter(sock.getOutputStream());
BufferedReader nhan= new BufferedReader(new InputStreamReader(sock.getInputStream()));
String data= nhan.readLine();
System.out.print(data.toString());
BufferedReader nhap = new BufferedReader(new InputStreamReader(System.in));
String username=nhap.readLine();
gui.write(username+"\n");
gui.flush();
BufferedReader nhan1= new BufferedReader(new InputStreamReader(sock.getInputStream()));
String data1= nhan1.readLine();
System.out.print(data1.toString());
BufferedReader nhap1 = new BufferedReader(new InputStreamReader(System.in));
String password=nhap1.readLine().toString().trim();
gui.write(password+"\n");
gui.flush();
}
}
 

Joe

Thành viên VIP
21/1/13
2,732
1,255
113
You can't fly when you just think you had 2 wings. Where did you instantiate the "con" ? And when you did, were you sure that NO exeception occurred?
 
Sửa lần cuối:
  • Like
Reactions: quanjoker