生活随笔
收集整理的這篇文章主要介紹了
实验六JDBC数据库操作_JAVA
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
實(shí)驗(yàn)?zāi)康?#xff1a;
1、熟悉數(shù)據(jù)庫(kù)基本操作 2、掌握利用JDBC進(jìn)行數(shù)據(jù)庫(kù)的連接 3、利用語(yǔ)句對(duì)象Statement和PreparedStatement對(duì)表、記錄、列進(jìn)行增、刪、改、查等操作 4、將數(shù)據(jù)庫(kù)操作封裝成類 5、了解單元測(cè)試Junit 6、掌握事務(wù)的概念,在編程中處理事務(wù) 7、掌握三層架構(gòu)編程思想
實(shí)驗(yàn)內(nèi)容:
1、JDBC單表記錄的增刪改查
已知:建立表student,并向表里插入幾條記錄,
create table
student (
id
int PRIMARY KEY auto_increment
,
name
varchar ( 20 ) not null
,
schoolin date not null
,
score
float not null
) ;
insert into student
values ( null
, ’李麗’
, ’
2015 - 09 - 01 ’
, 86 ) ;
insert into student
values ( null
, ’王五’
, ’
2016 - 09 - 01 ’
, 99 ) ;
insert into student
values ( null
, ’張三’
, ’
2014 - 09 - 01 ’
, 88 ) ;
要求:用JAVA程序?qū)崿F(xiàn)如下功能: 1、向表中增加記錄并顯示所有記錄(數(shù)據(jù)自己指定); 2、從表中刪除id=1的記錄并顯示所有記錄; 3、修改表中記錄:查詢條件id=2,將name修改為:山東理工,修改完畢顯示所有記錄; 4、查詢表中id=3的記錄并顯示;
package sdut
. cn
. test
; import java
. sql
. Timestamp
;
import java
. sql
. Connection
;
import java
. sql
. DriverManager
;
import java
. sql
. PreparedStatement
;
import java
. sql
. ResultSet
;
import java
. sql
. SQLException
;
import java
. text
. ParseException
;
import java
. text
. SimpleDateFormat
; import java
. util
. Date
; public class JDBCTest { public static void main ( String
[ ] args
) { try { addStudent ( "李麗" , Change ( "2015-09-01" ) , 86 ) ; addStudent ( "王五" , Change ( "2016-09-01" ) , 99 ) ; addStudent ( "張三" , Change ( "2014-09-01" ) , 88 ) ; addStudent ( "郭老師" , Change ( "2020-09-01" ) , 62 ) ; selectAll ( ) ; deleteStudent ( 1 ) ; selectAll ( ) ; updateStudent ( "山東理工" , 3 ) ; selectAll ( ) ; selectOne ( 3 ) ; } catch ( ClassNotFoundException e
) { e
. printStackTrace ( ) ; } catch ( SQLException e
) { e
. printStackTrace ( ) ; } } private static Timestamp
Change ( String time
) { SimpleDateFormat sdf
= new SimpleDateFormat ( "yyyy-MM-dd" ) ; Date date
= null
; try { date
= sdf
. parse ( time
) ; } catch ( ParseException e
) { e
. printStackTrace ( ) ; } Timestamp ts
= new Timestamp ( date
. getTime ( ) ) ; return ts
; } private static void addStudent ( String name
, Timestamp ts
, float score
) throws ClassNotFoundException
, SQLException
{ Class
. forName ( "com.mysql.jdbc.Driver" ) ; Connection con
= DriverManager
. getConnection ( "jdbc:mysql://localhost:3307/school?useUnicode=true&characterEncoding=utf-8" , "root" , "usbw" ) ; String sql
= "insert into student values(null,?,?,?)" ; PreparedStatement pst
= con
. prepareStatement ( sql
) ; pst
. setString ( 1 , name
) ; pst
. setTimestamp ( 2 , ts
) ; pst
. setFloat ( 3 , score
) ; int result
= pst
. executeUpdate ( ) ; pst
. close ( ) ; con
. close ( ) ; System
. out
. println ( result
> 0 ? "增加記錄成功!" : "沒(méi)有增加成功!" ) ; } private static void deleteStudent ( int id
) throws ClassNotFoundException
, SQLException
{ Class
. forName ( "com.mysql.jdbc.Driver" ) ; Connection con
= DriverManager
. getConnection ( "jdbc:mysql://localhost:3307/school?useUnicode=true&characterEncoding=utf-8" , "root" , "usbw" ) ; String sql
= "delete from student where id=?" ; PreparedStatement pst
= con
. prepareStatement ( sql
) ; pst
. setInt ( 1 , id
) ; int result
= pst
. executeUpdate ( ) ; pst
. close ( ) ; con
. close ( ) ; System
. out
. println ( result
> 0 ? "刪除記錄成功!" : "沒(méi)有刪除成功!" ) ; } private static void updateStudent ( String name
, int id
) throws ClassNotFoundException
, SQLException
{ Class
. forName ( "com.mysql.jdbc.Driver" ) ; Connection con
= DriverManager
. getConnection ( "jdbc:mysql://localhost:3307/school?useUnicode=true&characterEncoding=utf-8" , "root" , "usbw" ) ; String sql
= "update student set name = ? where id = ?" ; PreparedStatement pst
= con
. prepareStatement ( sql
) ; pst
. setString ( 1 , name
) ; pst
. setInt ( 2 , id
) ; int result
= pst
. executeUpdate ( ) ; pst
. close ( ) ; con
. close ( ) ; System
. out
. println ( result
> 0 ? "修改記錄成功!" : "沒(méi)有修改成功!" ) ; } private static void selectOne ( int id
) throws SQLException
, ClassNotFoundException
{ Class
. forName ( "com.mysql.jdbc.Driver" ) ; Connection con
= DriverManager
. getConnection ( "jdbc:mysql://localhost:3307/school?useUnicode=true&characterEncoding=utf-8" , "root" , "usbw" ) ; String sql
= "select * from student where id = ?" ; PreparedStatement pst
= con
. prepareStatement ( sql
) ; pst
. setInt ( 1 , id
) ; ResultSet rs
= pst
. executeQuery ( ) ; while ( rs
. next ( ) ) { int id1
= rs
. getInt ( "id" ) ; String name
= rs
. getString ( "name" ) ; Date date
= rs
. getDate ( "schoolin" ) ; float score
= rs
. getFloat ( "score" ) ; System
. out
. println ( id1
+ " " + name
+ " " + date
+ " " + score
) ; } rs
. close ( ) ; pst
. close ( ) ; con
. close ( ) ; } private static void selectAll ( ) throws SQLException
, ClassNotFoundException
{ Class
. forName ( "com.mysql.jdbc.Driver" ) ; Connection con
= DriverManager
. getConnection ( "jdbc:mysql://localhost:3307/school?useUnicode=true&characterEncoding=utf-8" , "root" , "usbw" ) ; String sql
= "select * from student" ; PreparedStatement pst
= con
. prepareStatement ( sql
) ; ResultSet rs
= pst
. executeQuery ( ) ; while ( rs
. next ( ) ) { int id
= rs
. getInt ( "id" ) ; String name
= rs
. getString ( "name" ) ; Date date
= rs
. getDate ( "schoolin" ) ; float score
= rs
. getFloat ( "score" ) ; System
. out
. println ( id
+ " " + name
+ " " + date
+ " " + score
) ; } rs
. close ( ) ; pst
. close ( ) ; con
. close ( ) ; } }
2、JDBC面向?qū)ο蠓绞綄?shí)現(xiàn)數(shù)據(jù)庫(kù)CRUD操作
已知:建立表student,并向表里插入幾條記錄。
create table
student (
id
int PRIMARY KEY auto_increment
,
name
varchar ( 20 ) not null
,
schoolin date not null
,
score
float not null
) ;
insert into student
values ( null
, ’李麗’
, ’
2015 - 09 - 01 ’
, 86 ) ;
insert into student
values ( null
, ’王五’
, ’
2016 - 09 - 01 ’
, 99 ) ;
insert into student
values ( null
, ’張三’
, ’
2014 - 09 - 01 ’
, 88 ) ;
基本要求:將表操作封裝成類,將功能封裝成類的方法。 要求:用JAVA程序?qū)崿F(xiàn)如下功能: 1、向表中增加記錄并顯示所有記錄(數(shù)據(jù)自己指定); 2、從表中刪除id=1的記錄并顯示所有記錄; 3、修改表中記錄:查詢條件id=2,將name修改為:山東理工,修改完畢顯示所有記錄; 4、查詢表中id=3的記錄并顯示;
(
1 )StudentDao類
package sdut
. cn
. studentdao
; import java
. sql
. Connection
;
import java
. sql
. DriverManager
;
import java
. sql
. ResultSet
;
import java
. sql
. SQLException
;
import java
. sql
. PreparedStatement
;
import java
. sql
. Timestamp
;
import java
. text
. ParseException
;
import java
. text
. SimpleDateFormat
;
import java
. util
. Date
; public class StudentDao { Connection con
; PreparedStatement pst
; ResultSet rs
; public Connection
getConnection ( ) throws ClassNotFoundException
, SQLException
{ Class
. forName ( "com.mysql.jdbc.Driver" ) ; Connection con
= DriverManager
. getConnection ( "jdbc:mysql://localhost:3307/school?useUnicode=true&characterEncoding=utf-8" , "root" , "usbw" ) ; return con
; } public void closeAll ( ) { try { if ( rs
!= null
) rs
. close ( ) ; if ( pst
!= null
) pst
. close ( ) ; if ( con
!= null
) con
. close ( ) ; } catch ( SQLException e
) { e
. printStackTrace ( ) ; } } public Timestamp
Change ( String time
) { SimpleDateFormat sdf
= new SimpleDateFormat ( "yyyy-MM-dd" ) ; Date date
= null
; try { date
= sdf
. parse ( time
) ; } catch ( ParseException e
) { e
. printStackTrace ( ) ; } Timestamp ts
= new Timestamp ( date
. getTime ( ) ) ; return ts
; } public void addStudent ( String name
, Timestamp ts
, float score
) throws ClassNotFoundException
, SQLException
{ con
= getConnection ( ) ; String sql
= "insert into student values(null,?,?,?)" ; pst
= con
. prepareStatement ( sql
) ; pst
. setString ( 1 , name
) ; pst
. setTimestamp ( 2 , ts
) ; pst
. setFloat ( 3 , score
) ; int result
= pst
. executeUpdate ( ) ; closeAll ( ) ; System
. out
. println ( result
> 0 ? "增加記錄成功!" : "沒(méi)有增加成功!" ) ; } public void deleteStudent ( int id
) throws ClassNotFoundException
, SQLException
{ con
= getConnection ( ) ; String sql
= "delete from student where id=?" ; pst
= con
. prepareStatement ( sql
) ; pst
. setInt ( 1 , id
) ; int result
= pst
. executeUpdate ( ) ; closeAll ( ) ; System
. out
. println ( result
> 0 ? "刪除記錄成功!" : "沒(méi)有刪除成功!" ) ; } public void updateStudent ( String name
, int id
) throws ClassNotFoundException
, SQLException
{ con
= getConnection ( ) ; String sql
= "update student set name = ? where id = ?" ; pst
= con
. prepareStatement ( sql
) ; pst
. setString ( 1 , name
) ; pst
. setInt ( 2 , id
) ; int result
= pst
. executeUpdate ( ) ; closeAll ( ) ; System
. out
. println ( result
> 0 ? "修改記錄成功!" : "沒(méi)有修改成功!" ) ; } public void selectOne ( int id
) throws SQLException
, ClassNotFoundException
{ con
= getConnection ( ) ; String sql
= "select * from student where id = ?" ; pst
= con
. prepareStatement ( sql
) ; pst
. setInt ( 1 , id
) ; ResultSet rs
= pst
. executeQuery ( ) ; while ( rs
. next ( ) ) { int id1
= rs
. getInt ( "id" ) ; String name
= rs
. getString ( "name" ) ; Date date
= rs
. getDate ( "schoolin" ) ; float score
= rs
. getFloat ( "score" ) ; System
. out
. println ( id1
+ " " + name
+ " " + date
+ " " + score
) ; } closeAll ( ) ; } public void selectAll ( ) throws SQLException
, ClassNotFoundException
{ con
= getConnection ( ) ; String sql
= "select * from student" ; pst
= con
. prepareStatement ( sql
) ; ResultSet rs
= pst
. executeQuery ( ) ; while ( rs
. next ( ) ) { int id
= rs
. getInt ( "id" ) ; String name
= rs
. getString ( "name" ) ; Date date
= rs
. getDate ( "schoolin" ) ; float score
= rs
. getFloat ( "score" ) ; System
. out
. println ( id
+ " " + name
+ " " + date
+ " " + score
) ; } closeAll ( ) ; }
} (
2 )StudentDaoTest類
package sdut
. cn
. studenttest
; import java
. sql
. SQLException
;
import sdut
. cn
. studentdao
. StudentDao
; public class StudentDaoTest { public static void main ( String
[ ] args
) { StudentDao sDao
= new StudentDao ( ) ; try { sDao
. addStudent ( "李麗" , sDao
. Change ( "2015-09-01" ) , 86 ) ; sDao
. addStudent ( "王五" , sDao
. Change ( "2016-09-01" ) , 99 ) ; sDao
. addStudent ( "張三" , sDao
. Change ( "2014-09-01" ) , 88 ) ; sDao
. addStudent ( "郭老師" , sDao
. Change ( "2020-09-01" ) , 62 ) ; sDao
. selectAll ( ) ; sDao
. deleteStudent ( 1 ) ; sDao
. selectAll ( ) ; sDao
. updateStudent ( "山東理工" , 3 ) ; sDao
. selectAll ( ) ; sDao
. selectOne ( 3 ) ; } catch ( ClassNotFoundException e
) { e
. printStackTrace ( ) ; } catch ( SQLException e
) { e
. printStackTrace ( ) ; } } }
3、簡(jiǎn)易版銀行管理管理
已知:現(xiàn)有賬戶表account,創(chuàng)建的SQL語(yǔ)句如下。
create table
account (
id
int PRIMARY KEY auto_increment
,
name
varchar ( 20 ) not null
,
money
double not null
) ;
基本要求: (1)利用面向?qū)ο蠓绞綄?shí)現(xiàn)功能; (2)利用三層架構(gòu)實(shí)現(xiàn); (3)應(yīng)用事務(wù)處理。 功能要求: (1)開(kāi)戶——增加記錄 增加3個(gè)賬戶,姓名分別為:張三、李四、王五,賬戶初始金額:0元; 開(kāi)戶成功,顯示所有賬戶信息。 (2)銷戶——?jiǎng)h除記錄 對(duì)姓名為"王五"的賬戶給予銷戶。成功操作后,顯示所有賬戶信息。 (3)存錢——修改記錄 張三、李四賬戶分別存入2000元。成功操作后,顯示所有賬戶信息。 (4)取錢——修改記錄 張三賬戶取出1000元,顯示張三賬戶信息。 (5)轉(zhuǎn)賬——修改記錄 李四給張三轉(zhuǎn)賬500元,顯示張三和李四賬戶信息。 (6)查詢所有賬戶信息.
//框架:
package cn
. sdut
. amain
; import java
. util
. Scanner
;
import cn
. sdut
. biz
. BankBiz
; public class Main { public static void main ( String
[ ] args
) { Scanner reader
= new Scanner ( System
. in
) ; BankBiz biz
= new BankBiz ( ) ; System
. out
. println ( "*************" ) ; int n
= reader
. nextInt ( ) ; reader
. nextLine ( ) ; for ( int i
= 0 ; i
< n
; i
++ ) { String name
= reader
. next ( ) ; double money
= reader
. nextDouble ( ) ; if ( biz
. openAccount ( name
, money
) ) System
. out
. println ( name
+ "開(kāi)戶成功!歡迎使用本銀行" ) ; else System
. out
. println ( name
+ "開(kāi)戶失敗!請(qǐng)聯(lián)系工作人員" ) ; } biz
. selectAccount ( ) ; System
. out
. println ( "*************" ) ; if ( biz
. delAccount ( "王五" ) ) System
. out
. println ( "王五銷戶成功!感謝您之前對(duì)本銀行的支持" ) ; else System
. out
. println ( "王五銷戶失敗!請(qǐng)聯(lián)系工作人員" ) ; biz
. selectAccount ( ) ; System
. out
. println ( "*************" ) ; if ( biz
. addUpdateAccount ( "張三" , 2000 ) ) System
. out
. println ( "恭喜您存錢成功" ) ; else System
. out
. println ( "存錢失敗" ) ; if ( biz
. addUpdateAccount ( "李四" , 2000 ) ) System
. out
. println ( "恭喜您存錢成功!" ) ; else System
. out
. println ( "存錢失敗!請(qǐng)聯(lián)系工作人員" ) ; biz
. selectAccount ( ) ; System
. out
. println ( "*************" ) ; if ( biz
. delUpdateAccount ( "張三" , 1000 ) ) System
. out
. println ( "張三取錢成功!" ) ; else System
. out
. println ( "張三取錢失敗!請(qǐng)聯(lián)系工作人員" ) ; biz
. selectOneAccount ( "張三" ) ; System
. out
. println ( "*************" ) ; if ( biz
. tranAccount ( "李四" , "張三" , 500 ) ) System
. out
. println ( "轉(zhuǎn)賬成功!" ) ; else System
. out
. println ( "轉(zhuǎn)賬失敗!請(qǐng)聯(lián)系工作人員" ) ; biz
. selectOneAccount ( "張三" ) ; biz
. selectOneAccount ( "李四" ) ; System
. out
. println ( "*************" ) ; biz
. selectAccount ( ) ; reader
. close ( ) ; } }
package cn
. sdut
. biz
; import java
. sql
. SQLException
;
import cn
. sdut
. dao
. AccountDao
;
import cn
. sdut
. entity
. Account
; public class BankBiz { AccountDao dao
= new AccountDao ( ) ; public boolean openAccount ( String name
, double money
) { int result
= 0 ; try { Account account
= new Account ( ) ; account
. setName ( name
) ; account
. setMoney ( money
) ; result
= dao
. addAccount ( account
) ; } catch ( ClassNotFoundException e
) { e
. printStackTrace ( ) ; } catch ( SQLException e
) { e
. printStackTrace ( ) ; } return result
> 0 ? true : false ; } public boolean delAccount ( String name
) { int result
= 0 ; try { result
= dao
. delAccount ( name
) ; } catch ( ClassNotFoundException e
) { e
. printStackTrace ( ) ; } catch ( SQLException e
) { e
. printStackTrace ( ) ; } return result
> 0 ? true : false ; } public boolean addUpdateAccount ( String name
, double money
) { int result
= 0 ; try { result
= dao
. addUpdateAccount ( name
, money
) ; } catch ( ClassNotFoundException e
) { e
. printStackTrace ( ) ; } catch ( SQLException e
) { e
. printStackTrace ( ) ; } return result
> 0 ? true : false ; } public boolean delUpdateAccount ( String name
, double money
) { int result
= 0 ; try { result
= dao
. delUpdateAccount ( name
, money
) ; } catch ( ClassNotFoundException e
) { e
. printStackTrace ( ) ; } catch ( SQLException e
) { e
. printStackTrace ( ) ; } return result
> 0 ? true : false ; } public boolean tranAccount ( String fromname
, String toname
, double money
) { boolean result
= false ; try { result
= dao
. tranAccount ( fromname
, toname
, money
) ; } catch ( ClassNotFoundException e
) { e
. printStackTrace ( ) ; } catch ( SQLException e
) { e
. printStackTrace ( ) ; } return result
; } public void selectAccount ( ) { try { dao
. selectAccount ( ) ; } catch ( ClassNotFoundException e
) { e
. printStackTrace ( ) ; } catch ( SQLException e
) { e
. printStackTrace ( ) ; } } public void selectOneAccount ( String name
) { try { dao
. selectOneAccount ( name
) ; } catch ( ClassNotFoundException e
) { e
. printStackTrace ( ) ; } catch ( SQLException e
) { e
. printStackTrace ( ) ; } } }
package cn
. sdut
. dao
; import java
. sql
. Connection
;
import java
. sql
. DriverManager
;
import java
. sql
. PreparedStatement
;
import java
. sql
. ResultSet
;
import java
. sql
. SQLException
; public class BaseDao { Connection con
; PreparedStatement pst
; ResultSet rs
; public Connection
getConnection ( ) throws ClassNotFoundException
, SQLException
{ Class
. forName ( "com.mysql.jdbc.Driver" ) ; Connection con
= DriverManager
. getConnection ( "jdbc:mysql://localhost:3307/bank?useUnicode=true&characterEncoding=utf-8" , "root" , "usbw" ) ; return con
; } public void closeAll ( ) throws SQLException
{ if ( rs
!= null
) rs
. close ( ) ; if ( pst
!= null
) pst
. close ( ) ; if ( con
!= null
) con
. close ( ) ; }
} package cn
. sdut
. dao
; import java
. sql
. SQLException
; import cn
. sdut
. entity
. Account
; public interface AccountDaoInterface { public int addAccount ( Account account
) throws ClassNotFoundException
, SQLException
; public int delAccount ( String name
) throws ClassNotFoundException
, SQLException
; public int addUpdateAccount ( String name
, double money
) throws ClassNotFoundException
, SQLException
; public int delUpdateAccount ( String name
, double money
) throws ClassNotFoundException
, SQLException
; public boolean tranAccount ( String fromname
, String toname
, double money
) throws ClassNotFoundException
, SQLException
; public void selectAccount ( ) throws ClassNotFoundException
, SQLException
; public void selectOneAccount ( String name
) throws ClassNotFoundException
, SQLException
;
} package cn
. sdut
. dao
; import java
. sql
. SQLException
;
import java
. util
. ArrayList
;
import java
. util
. List
;
import cn
. sdut
. entity
. Account
; public class AccountDao extends BaseDao implements AccountDaoInterface { public int addAccount ( Account account
) throws ClassNotFoundException
, SQLException
{ con
= getConnection ( ) ; String sql
= "insert into account values(null,?,?)" ; pst
= con
. prepareStatement ( sql
) ; pst
. setString ( 1 , account
. getName ( ) ) ; pst
. setDouble ( 2 , account
. getMoney ( ) ) ; int result
= pst
. executeUpdate ( ) ; closeAll ( ) ; return result
; } public int delAccount ( String name
) throws ClassNotFoundException
, SQLException
{ con
= getConnection ( ) ; String sql
= "delete from account where name=?" ; pst
= con
. prepareStatement ( sql
) ; pst
. setString ( 1 , name
) ; int result
= pst
. executeUpdate ( ) ; closeAll ( ) ; return result
; } public int addUpdateAccount ( String name
, double money
) throws ClassNotFoundException
, SQLException
{ con
= getConnection ( ) ; String sql
= "update account set money=money+? where name=?" ; pst
= con
. prepareStatement ( sql
) ; pst
. setString ( 2 , name
) ; pst
. setDouble ( 1 , money
) ; int result
= pst
. executeUpdate ( ) ; closeAll ( ) ; return result
; } public int delUpdateAccount ( String name
, double money
) throws ClassNotFoundException
, SQLException
{ con
= getConnection ( ) ; String sql
= "update account set money = money - ? where name = ?" ; pst
= con
. prepareStatement ( sql
) ; pst
. setDouble ( 1 , money
) ; pst
. setString ( 2 , name
) ; int result
= pst
. executeUpdate ( ) ; closeAll ( ) ; return result
; } public boolean tranAccount ( String fromname
, String toname
, double money
) throws ClassNotFoundException
, SQLException
{ con
= getConnection ( ) ; con
. setAutoCommit ( false ) ; String sql1
= "update account set money = money - ? where name = ?" ; pst
= con
. prepareStatement ( sql1
) ; pst
. setString ( 2 , fromname
) ; pst
. setDouble ( 1 , money
) ; int result1
= pst
. executeUpdate ( ) ; String sql2
= "update account set money=money+? where name=?" ; pst
= con
. prepareStatement ( sql2
) ; pst
. setString ( 2 , toname
) ; pst
. setDouble ( 1 , money
) ; int result2
= pst
. executeUpdate ( ) ; int result
= result1
* result2
; if ( result
== 1 ) { con
. commit ( ) ; } else { con
. rollback ( ) ; } closeAll ( ) ; return result
> 0 ? true : false ; } public void selectAccount ( ) throws ClassNotFoundException
, SQLException
{ con
= getConnection ( ) ; String sql
= "select * from account" ; pst
= con
. prepareStatement ( sql
) ; rs
= pst
. executeQuery ( ) ; List
< Account> accountList
= new ArrayList < Account> ( ) ; while ( rs
. next ( ) ) { Account account
= new Account ( ) ; account
. setId ( rs
. getInt ( "id" ) ) ; account
. setName ( rs
. getString ( "name" ) ) ; account
. setMoney ( rs
. getDouble ( "money" ) ) ; accountList
. add ( account
) ; } closeAll ( ) ; for ( Account ac
: accountList
) { System
. out
. println ( ac
) ; } } public void selectOneAccount ( String name
) throws ClassNotFoundException
, SQLException
{ con
= getConnection ( ) ; String sql
= "select * from account where name=?" ; pst
= con
. prepareStatement ( sql
) ; pst
. setString ( 1 , name
) ; rs
= pst
. executeQuery ( ) ; Account account
= null
; if ( rs
. next ( ) ) { account
= new Account ( ) ; account
. setId ( rs
. getInt ( "id" ) ) ; account
. setName ( rs
. getString ( "name" ) ) ; account
. setMoney ( rs
. getDouble ( "money" ) ) ; } closeAll ( ) ; System
. out
. println ( account
) ; } }
package cn
. sdut
. entity
; public class Account { private int id
; private String name
; private double money
; public int getId ( ) { return id
; } public void setId ( int id
) { this . id
= id
; } public String
getName ( ) { return name
; } public void setName ( String name
) { this . name
= name
; } public double getMoney ( ) { return money
; } public void setMoney ( double money
) { this . money
= money
; } @Override public String
toString ( ) { return "Account [id=" + id
+ ", name=" + name
+ ", money=" + money
+ "]" ; }
}
總結(jié)
以上是生活随笔 為你收集整理的实验六JDBC数据库操作_JAVA 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。