#2
mywisdom882017-10-13 17:47
|
程序代码:
package dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import dao.UserDao;
public class UserDaoImpl implements UserDao{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
@Override
public int insert(String username,String password) {
int i=0;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","user2","admin");
pstmt=conn.prepareStatement("insert into users values(?,?)");
pstmt.setString(1,username);
pstmt.setString(2,password);
i = pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
@Override
public boolean login(String username,String password) {
boolean b=false;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","user2","admin");
pstmt=conn.prepareStatement("select * from users where username=? and password=?");
pstmt.setString(1,username);
pstmt.setString(2,password);
//pstmt 执行sql语句
//pstmt.execute();//适用于增 删 改 查
//pstmt.executeUpdate();//适用于 增 删 改 ==>int 受影响的行数
rs=pstmt.executeQuery();//适用于 select ==>resultSet
if(rs.next()){
b=true;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return b;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import dao.UserDao;
public class UserDaoImpl implements UserDao{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
@Override
public int insert(String username,String password) {
int i=0;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","user2","admin");
pstmt=conn.prepareStatement("insert into users values(?,?)");
pstmt.setString(1,username);
pstmt.setString(2,password);
i = pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
@Override
public boolean login(String username,String password) {
boolean b=false;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","user2","admin");
pstmt=conn.prepareStatement("select * from users where username=? and password=?");
pstmt.setString(1,username);
pstmt.setString(2,password);
//pstmt 执行sql语句
//pstmt.execute();//适用于增 删 改 查
//pstmt.executeUpdate();//适用于 增 删 改 ==>int 受影响的行数
rs=pstmt.executeQuery();//适用于 select ==>resultSet
if(rs.next()){
b=true;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(rs!=null)rs.close();
if(pstmt!=null)pstmt.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return b;
}
}
只有本站会员才能查看附件,请 登录
求助求助