자바 코드 로 데이터베이스 연결 작업 작성
8041 단어 JDBC
package JDBC_Manager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.concurrent.ExecutionException;
import javax.swing.text.DefaultEditorKit.PasteAction;
import org.w3c.dom.html.HTMLHRElement;
import com.sun.corba.se.impl.orbutil.RepositoryIdStrings;
import com.sun.rmi.rmid.ExecPermission;
import Day1.PrepareStatement;
/**
*
, 、 、 、
*
*
*/
public class AccountDAO {
//1、 , 。
public Account queryAccount(String card_id)throws Exception{
Connection conn=null;
PreparedStatement pstm=null;
ResultSet rest=null;
//①、
Accountutil accountutil=new Accountutil();
conn=accountutil.getconnection();
//②、 sql
String str="select card_id,card_passward,card_balance,card_name,card_phone from Account where card_id=?";
//③、 sql
pstm=conn.prepareStatement(str);
pstm.setString(1, card_id);
rest=pstm.executeQuery();
Account acc=new Account();
//④、
while(rest.next()){
String card_passward=rest.getString("card_passward");
double card_balance=rest.getDouble("card_balance");
String card_name=rest.getString("card_name");
String card_phone=rest.getString("card_phone");
acc.setCard_balance(card_balance);
acc.setCard_name(card_name);
acc.setCard_passward(card_passward);
acc.setCard_phone(card_phone);
acc.setCard_id(card_id);
}
//
accountutil.closeconnection(conn, pstm, rest);
return acc;
}
/**
* 2、 , 。
*/
public void UpdateAccount(Account account)throws Exception{
Connection con=null;
PreparedStatement past=null;
//①、
Accountutil accountutil=new Accountutil();
con=accountutil.getconnection();
String str1=account.getCard_passward();
double dou1=account.getCard_balance();
String updatesql="update Account set card_passward=? ,card_balance=? ,card_name=? ,card_phone=? where card_id=?";
past=con.prepareStatement(updatesql);
past.setString(1, account.getCard_passward());
past.setDouble(2,account.getCard_balance());
past.setString(3, account.getCard_name());
past.setString(4, account.getCard_phone());
past.setString(5, account.getCard_id());
past.executeUpdate();
accountutil.closeconnection(con, past, null);
}
//3、 ,
public void deleteAccount(String a)throws Exception{
Connection conn=null;
PreparedStatement pstm=null;
//①、
Accountutil accountutil=new Accountutil();
conn=accountutil.getconnection();
String deletesql="delete from Account where card_id=?";
pstm=conn.prepareStatement(deletesql);
pstm.setString(1, a);
pstm.executeUpdate();
accountutil.closeconnection(conn, pstm, null);
}
//4、 , 。
public void addAccount(Account acc)throws Exception{
Connection conn=null;
PreparedStatement pstm=null;
ResultSet rest=null;
//①、
Accountutil accountutil=new Accountutil();
conn=accountutil.getconnection();
String str2="select seq_newsid.nextval from sys.dual";
pstm=conn.prepareStatement(str2);
rest=pstm.executeQuery();
String str3 = null;
while(rest.next()){
str3=rest.getString(1);
}
String str="insert into Account(card_id,card_passward,card_balance,card_name,card_phone)values(?,?,?,?,?)";
pstm=conn.prepareStatement(str);
acc.setCard_id(str3);
pstm.setString(1, str3);
pstm.setString(2,acc.getCard_passward() );
pstm.setDouble(3, acc.getCard_balance());
pstm.setString(4, acc.getCard_name());
pstm.setString(5, acc.getCard_phone());
pstm.executeUpdate();
accountutil.closeconnection(conn, pstm, rest);
}
}
//
package JDBC_Manager;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import Day1.PrepareStatement;
public class JDBCutil2 {
private static final Properties prop=new Properties();
/**
* properties map , properties ,
* .properties 。 url、DriverManager 。
*/
static InputStream is=null;
static{
try {
is=JDBCutil2.class.getResourceAsStream("/JDBC_Manager/JDBC.properties");
prop.load(is);
String str1=prop.getProperty("DriverManager");
Class.forName(str1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getconnection(){
Connection con=null;
try {
String str2=prop.getProperty("url");
String str3=prop.getProperty("user");
String str4=prop.getProperty("password");
con=DriverManager.getConnection(str2,str3,str4);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public void closeconnection(Connection con,PreparedStatement pstm,ResultSet rest){
try {
rest.close();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
rest.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package JDBC_Manager;
//
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import Day1.PrepareStatement;
public class JDBCutil2 {
private static final Properties prop=new Properties();
/**
* properties map , properties ,
* .properties 。 url、DriverManager 。
*/
static InputStream is=null;
static{
try {
is=JDBCutil2.class.getResourceAsStream("/JDBC_Manager/JDBC.properties");
prop.load(is);
String str1=prop.getProperty("DriverManager");
Class.forName(str1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getconnection(){
Connection con=null;
try {
String str2=prop.getProperty("url");
String str3=prop.getProperty("user");
String str4=prop.getProperty("password");
con=DriverManager.getConnection(str2,str3,str4);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public void closeconnection(Connection con,PreparedStatement pstm,ResultSet rest){
try {
rest.close();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
rest.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//JDBC properties JDBC.properties
DriverManager=oracle.jdbc.OracleDriver
user=hr
password=wpp
url=jdbc:oracle:thin:@127.0.0.1:1521:xe
// ,
package JDBC_Manager;
import java.util.concurrent.ExecutionException;
public class Text_account {
public static void main(String[] args)throws Exception{
/*AccountDAO ado=new AccountDAO();
Account acc=new Account();
String str="999";
acc=ado.queryAccount(str);
System.out.println(acc.getCard_balance()+" "+acc.getCard_name()+acc.getCard_phone());
*/
/*
AccountDAO ado=new AccountDAO();
Account acc=new Account("999","abc",888.0,"wpp","18839788888");
ado.UpdateAccount(acc);
*/
/*String aString="999";
AccountDAO ado=new AccountDAO();
ado.deleteAccount(aString);
*/
Account account=new Account("null","abc",999.0,"wpp","18839788028");
AccountDAO accDAO=new AccountDAO();
accDAO.addAccount(account);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
데이터 시각화 도구 FineReport와 AWS RedShift 연결(JDBC 방법)Amazon Redshift는 클라우드의 완전 관리형, 페타바이트 규모 데이터 웨어하우스 서비스입니다. 수백 기가바이트의 데이터로 시작하여 페타바이트 이상까지 확장할 수 있습니다. 이렇게 하면 고객의 비즈니스와 고객...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.