SQL 서버 사용
DBCon.java
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author Administrator
*/
public class DBCon {
public static Connection getConnection(){
Connection dbConnection = null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dbConnection = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=firstdb",
"sa", "123");
System.out.println(" ");
}catch(Exception e){
e.printStackTrace();
}
return dbConnection;
}
public static void closeConnection(Connection dbConnection){
try{
if(dbConnection != null && !dbConnection.isClosed()){
dbConnection.close();
System.out.println(" ");
}
}catch(SQLException sqlEx){
sqlEx.printStackTrace();
}
}
public static void closeStatement(PreparedStatement pStatement){
try{
if(pStatement != null){
pStatement.close();
pStatement = null;
}
}catch(SQLException sqlEx){
sqlEx.printStackTrace();
}
}
public static void closeResultSet(ResultSet res){
try{
if(res != null){
res.close();
res = null;
}
}catch(SQLException sqlEx){
sqlEx.printStackTrace();
}
}
public static void main(String[] args)
{
System.out.println(" ……");
Connection dbConnection = getConnection();
closeConnection(dbConnection);
}
}
UserDAO.java
package dao;
import static dao.DBCon.getConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import test.User;
/**
*
* @author ASUS
*/
public class UserDAO {
public static int insertOne(User em)
{
int result = 0;
Connection con = null;
PreparedStatement pst = null;
try{
con = getConnection();
String strSql = " insert into dbo.Tab1(username,password) values(?,?)";
pst = con.prepareStatement(strSql);
pst.setString(1, em.getUsername());
pst.setString(2, em.getPassword());
result = pst.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
DBCon.closeStatement(pst);
DBCon.closeConnection(con);
}
return result;
}
public static int deleteOne(String username)
{
int result = 0;
Connection con = null;
PreparedStatement pst = null;
try{
con = getConnection();
String strSql = " delete from dbo.Tab1 where username=?";
pst = con.prepareStatement(strSql);
pst.setString(1, username);
result = pst.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
DBCon.closeStatement(pst);
DBCon.closeConnection(con);
}
return result;
}
public static int updataOne(User em)
{
int result = 0;
Connection con = null;
PreparedStatement pst = null;
try{
con = getConnection();
String strSql = " update dbo.Tab1 set password=? where username=?";
pst = con.prepareStatement(strSql);
pst.setString(1, em.getPassword());
pst.setString(2, em.getUsername());
result = pst.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}finally{
DBCon.closeStatement(pst);
DBCon.closeConnection(con);
}
return result;
}
public static User queryOne(String us){
User em =null;
Connection con =null;
PreparedStatement pst = null;
ResultSet rst = null;
try{
con =DBCon.getConnection();
String sql = "select * from dbo.Tab1 where username=?";
pst = con.prepareStatement(sql);
pst.setString(1, us);
rst = pst.executeQuery();
if(rst.next()){
em = new User();
em.setUsername(rst.getString(1));
em.setPassword(rst.getString(2));
}
}catch(SQLException e){
e.printStackTrace();
}
finally{
DBCon.closeResultSet(rst);
DBCon.closeStatement(pst);
DBCon.closeConnection(con);
return em;
}
}
public static ArrayList queryMore(String us)
{
ArrayList empList = new ArrayList();
Connection con =null;
PreparedStatement pst = null;
ResultSet rst = null;
try{
con =DBCon.getConnection();
String sql = "select * from dbo.Tab1 where username=?";
pst = con.prepareStatement(sql);
pst.setString(1, us);
rst = pst.executeQuery();
while(rst.next()){
User em = new User();
em.setUsername(rst.getString(1));
em.setPassword(rst.getString(2));
empList.add(em);
}
}catch(SQLException e){
e.printStackTrace();
}
finally{
DBCon.closeResultSet(rst);
DBCon.closeStatement(pst);
DBCon.closeConnection(con);
return empList;
}
}
}
User.java
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User() {
}
}
Test.java
package test;
import dao.UserDAO;
import java.util.ArrayList;
import java.util.Iterator;
/**
*
* @author ASUS
*/
public class Test {
public static void main(String[] args)
{
System.out.println(" ……");
// UserDAO.deleteOne(" ");
// User em = new User("gqp","csb");
// UserDAO.insertOne(em);
// UserDAO.updataOne(em);
String us ="gqp";
// User em = UserDAO.queryOne(us);
// System.out.println(em.getUsername()+" "+em.getPassword());
ArrayList list = new ArrayList();
list = UserDAO.queryMore(us);
for (Iterator it = list.iterator(); it.hasNext();) {
User tmp = (User) it.next();
System.out.println(tmp.getUsername()+" "+tmp.getPassword());
}
System.out.println(" ");
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.