大家好我不熟悉Java并开始学习java / jsp / servlets这个项目。我有一个Oracle表(名为giris),其中包含用户名(kullanici)和密码(sifre)。
英语不是我的第一语言或第二语言,所以看看我的代码,你会理解我的斗争。
我有girisDTO.java用于获取set方法和构造函数:
package tr.gov.ptt.rehberwebprj.dto;
public class GirisDTO {
private String kullanici;
private String sifre;
public GirisDTO()
{
}
public String getKullanici() {
return kullanici;
}
public void setKullanici(String kullanici) {
this.kullanici = kullanici;
}
public String getSifre() {
return sifre;
}
public void setSifre(String sifre) {
this.sifre = sifre;
}
}
我有VTBaglanti.java用于数据库连接:
package tr.gov.ptt.rehberwebprj.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class VTBaglanti {
public static Connection baglantiGetir() throws ClassNotFoundException, SQLException
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "kurs1", "ptt");
return null;
}
public static void baglantiKapat(Connection p_conn) throws SQLException
{
p_conn.close();
}
}
我有GirisDAO.java用于连接以外的数据库操作:
package tr.gov.ptt.rehberwebprj.dao;
import java.sql.*;
import tr.gov.ptt.rehberwebprj.dto.GirisDTO;
import tr.gov.ptt.rehberwebprj.util.VTBaglanti;
public class GirisDAO {
public static boolean giriseYetkilimi(GirisDTO p_giris)
throws ClassNotFoundException, SQLException
{
String vtSifre=null;
Connection conn= VTBaglanti.baglantiGetir();
PreparedStatement ps = conn.prepareStatement("select sifre from giris where kullanici=?");
ps.setString(1, p_giris.getKullanici());
ResultSet rs = ps.executeQuery();
while(rs.next())
{
vtSifre=rs.getString("sifre");
}
rs.close();
if (vtSifre != null && vtSifre.equals(p_giris.getSifre()))
{
return true;
}
else
{
return false;
}
}
}
我写了一个GirisDAOTest.java类来测试GirisDAO是否正常工作但是它给了我一个错误。
package tr.gov.ptt.rehberwebprj.test;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import tr.gov.ptt.rehberwebprj.dao.GirisDAO;
import tr.gov.ptt.rehberwebprj.dto.GirisDTO;
public class GirisDAOTest {
public static void girisKontrolTest() {
try {
GirisDTO giris= new GirisDTO();
giris.setKullanici("eda");
giris.setSifre("1234");
boolean sonuc = GirisDAO.giriseYetkilimi(giris);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
if (true)
{
System.out.println("Giriş başarılı.");
} else
{
System.out.println("Kullanıcı adı ya da şifre yanlıs");
}
}
public static void main(String[] args) {
girisKontrolTest();
}
}
我的错误是:
Exception in thread "main" java.lang.NullPointerException
at tr.gov.ptt.rehberwebprj.dao.GirisDAO.giriseYetkilimi(GirisDAO.java:15)
at tr.gov.ptt.rehberwebprj.test.GirisDAOTest.girisKontrolTest(GirisDAOTest.java:20)
at tr.gov.ptt.rehberwebprj.test.GirisDAOTest.main(GirisDAOTest.java:41)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)