使用Java對(duì)數(shù)據(jù)庫(kù)進(jìn)行基本的查詢和更新操作
數(shù)據(jù)庫(kù)查詢
利用Connection對(duì)象的createStatement方法建立Statement對(duì)象,利用Statement對(duì)象的executeQuery()方法執(zhí)行SQL查詢語(yǔ)句進(jìn)行查詢,返回結(jié)果集,再形如getXXX()的方法從結(jié)果集中讀取數(shù)據(jù)。經(jīng)過(guò)這樣的一系列步驟就能實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的查詢。
【例】Java應(yīng)用程序訪問(wèn)數(shù)據(jù)庫(kù)。應(yīng)用程序打開(kāi)考生信息表ksInfo,從中取出考生的各項(xiàng)信息。設(shè)考生信息數(shù)據(jù)庫(kù)的結(jié)構(gòu)如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
public class Example10_9 extends JFrame implements ActionListener{
public static Connection connectByJdbcodbc(String url, String username,String password){
Connection con = null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加載ODBC驅(qū)動(dòng)程序
}
catch (Exception e){
e.printStackTrace();
return null; //加載失敗,連接不成功
}
try{
con = DriverManager.getConnection(url, username, password);
}
catch (SQLException e){
e.printStackTrace();
return null; //連接失敗
}
return con; //連接成功
}
String title[] ={"考號(hào)", "姓名", "成績(jī)", "地址", "簡(jiǎn)歷"};
JTextField txtNo = new JTextField(8);
JTextField txtName = new JTextField(10);
JTextField txtScore = new JTextField(3);
JTextField txtAddr = new JTextField(30);
JTextArea txtresume = new JTextArea();
JButton prev = new JButton("前一個(gè)");
JButton next = new JButton("后一個(gè)");
JButton first = new JButton("第一個(gè)");
JButton last = new JButton("最后一個(gè)");
Statement sql; //SQL語(yǔ)句對(duì)象
ResultSet rs; //存放查詢結(jié)果對(duì)象
Example10_9(Connection connect){
super("考生信息查看窗口");
setSize(450, 350);
try{
sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = sql.executeQuery("SELECT * FROM ksInfo");
Container con = getContentPane();
con.setLayout(new BorderLayout(0, 6); JPanel p[] = new JPanel[4];
for (int i = 0; i < 4; i++){
p[i] = new JPane(new FlowLayout(FlowLayout.LEFT, 8, 0));
p[i].add(new JLabel(title[i]));
}
p[0].add(txtNo);
p[1].add(txtName);
p[2].add(txtScore);
p[3].add(txtAddr);
JPanel p1 = new JPane(new GridLayout94, 1, 0, 8));
JScrollPane jsp = new JScrollPane(txtResume,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jsp.setPreforredSize(new Dimension(300, 60);
for (int i = 0; i < 4; i++){
p1.add(p[i]);
}
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0);
p2.add(new JLabel(title[4]));
p2.add(jsp);
Jpanel p3 = new Jpanel();
p3.add(prev);
p3.add(next);
p3.add(first);
p3.add(last);
prev.addActionListener(this);
next.addActionListener(this);
first.addActionListener(this);
last.addActionlistener(this);
rs.first();
readRecord();
}
catch (Exception e){
e.printStackTrace():
}
setVisible(ture);
}
public void modifyRecord(Connection connect){
String stuNo = (String)JOptionPane.showInputDialog(null,
"請(qǐng)輸入考生考號(hào)", "輸入考號(hào)對(duì)話框", JOptionPane.PLAIN_MESSAGE, null,
null, "");
try {
sql = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = sql.executeQuery("SELECT * FROM ksInfo");
Container con = getContentPane();
con.setLayout(new Boarderlayout(0, 6));
Jpanel p[] = new JPanel[4];
for (int i = 0; i < ; i++){
p[i] = new JPane(new FlowLayout(flowLayout.LEFT, 8, 0));
p[i].add(new JLabel(title[i]));
}
p[0].add(txtNo);
p[1].add(txtName);
p[2].add(txtScore);
p[3].add(txtAddr);
Jpanel p1 = new Jpane(new GridLayout(4, 1, 0, 8));
JScrollPane jsp = new JScrollPane(txtResume,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jsp.setPreferredSize (new dimension(300, 60));
for (int i = 0; i < 4; i++){
p1.add(p[i]);
}
Jpanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
p2.add(new JLableI(title[4]));
p2.add(jsp);
JPanel p3 = new JPanel();
p3.add(prev);
p3.add(next);
p3.add(first);
p3.add(last);
prev.addActionListener(this);
next.addActionListener(this);
first.addActionListenerIthis);
last.addActionListener(this);
rs.first();
readRecord();
}
catch (Exception e){
e.printStackTrace();
}
setVisible(true);
}
boolean readRecord(){
try{
txtNo.setText(rs.getString("考號(hào)")); txtName.setText(rs.getString("姓名")); txtScore.setText(rs.getString("成績(jī)"));
txtAddr.setText(rs.getString("地址")); txtResume.setText(rs.getString("簡(jiǎn)歷"));
}
catch (SQLException e){
e.printStackTrace(); return false;
}
return true;
}
public void actionPerformed(ActionEvent e){
try{
if (e.getSource() == prev)rs.previous();
else if (e.getSource() == next)rs.next();
else if (e.getSource() == first)rs.first();
else if (e.getSource() == last)rs.last(); readRecord();
}
catch (Exception e2){}
}
public static void main(String args[]){
connection connect = null;
JFrame .setDefaultLookAndFeeDecorated(true);
Font font = new Font("JFrame", Font.PLAIN, 14);
if ((connect =connectByJdbcOdbc("jdbc:odbc:redsun", "xia", "1234")) == null){
JOptionPane.showMessageDialog(null, "數(shù)據(jù)庫(kù)連接失敗!");
System.exit ( - 1);
}
new Example10_9(connect); //創(chuàng)建對(duì)象
}
}
Java數(shù)據(jù)庫(kù)更新
數(shù)據(jù)庫(kù)更新操作包括數(shù)據(jù)表創(chuàng)建、刪除、以及數(shù)據(jù)表記錄的增加、刪除、修改等操作。如果利用數(shù)據(jù) SQL命令實(shí)現(xiàn),則利用Statement對(duì)旬的executeUpdate()方法,執(zhí)行SQL的update語(yǔ)句,實(shí)現(xiàn)數(shù)據(jù)表的修改;執(zhí)行SQL的insert語(yǔ)句,實(shí)現(xiàn)數(shù)據(jù)表記錄的添加。
例如,在前面數(shù)據(jù)為查詢例子基礎(chǔ)上,再增加對(duì)數(shù)據(jù)表的修改和插入。限于篇幅,不再給出完整程序,只給出實(shí)現(xiàn)修改和插入的方法。程序可再增設(shè)插入、,,除保存按鈕,通過(guò)已有的瀏覽,定位到數(shù)據(jù)表的特定位置,對(duì)癰疽記錄進(jìn)行編輯修改,或插入,或刪除,然后按保存按鈕,完成修改后的數(shù)據(jù)表保存。
下面用代碼說(shuō)明數(shù)據(jù)表更新的方法。與數(shù)據(jù)表連接時(shí),需指定獲得的ResultSet 對(duì)象是可更新的。
stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
- 使用Java實(shí)現(xiàn)先查詢緩存再查詢數(shù)據(jù)庫(kù)
- Java對(duì)MySQL數(shù)據(jù)庫(kù)進(jìn)行連接、查詢和修改操作方法
- Java mysql數(shù)據(jù)庫(kù)并進(jìn)行內(nèi)容查詢實(shí)例代碼
- 在Java的Hibernate框架中對(duì)數(shù)據(jù)庫(kù)數(shù)據(jù)進(jìn)行查詢操作
- java 使用ElasticSearch完成百萬(wàn)級(jí)數(shù)據(jù)查詢附近的人功能
- java實(shí)現(xiàn)的連接數(shù)據(jù)庫(kù)及模糊查詢功能示例
- Java連接mysql數(shù)據(jù)庫(kù)并進(jìn)行內(nèi)容查詢的方法
- java查詢近七日數(shù)據(jù)功能的實(shí)現(xiàn)
相關(guān)文章
springmvc實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)信息為excle表格示例代碼
本篇文章主要介紹了springmvc實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)信息為excle表格,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。2017-01-01
一文詳解Java如何系統(tǒng)地避免空指針問(wèn)題
新手Java開(kāi)發(fā)總是經(jīng)??罩羔槞z查,甚至某些老手也會(huì)犯這樣的問(wèn)題,所以這篇文章小編就帶大家一起來(lái)看看如何系統(tǒng)地避免空指針問(wèn)題,希望對(duì)大家有所幫助2024-01-01
spring+maven實(shí)現(xiàn)郵件發(fā)送
這篇文章主要為大家詳細(xì)介紹了spring+maven實(shí)現(xiàn)郵件發(fā)送,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Java編程反射機(jī)制用法入門(mén)與實(shí)例總結(jié)
這篇文章主要介紹了Java編程反射機(jī)制用法,簡(jiǎn)單說(shuō)明了反射機(jī)制的概念、原理并結(jié)合實(shí)例形式總結(jié)分析了java反射機(jī)制的簡(jiǎn)單使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-12-12
SpringMVC注解@CrossOrigin跨域問(wèn)題詳解
這篇文章主要介紹了SpringMVC注解@CrossOrigin跨域問(wèn)題詳解,跨域是瀏覽同源策略的造成,是瀏覽器對(duì)JavaScript施加的安全限制CORS是一種可以解決跨域問(wèn)題的技術(shù),需要的朋友可以參考下2023-11-11
springboot自定義redis-starter的實(shí)現(xiàn)
這篇文章主要介紹了springboot自定義redis-starter的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
AJAX Servlet實(shí)現(xiàn)數(shù)據(jù)異步交互的方法
本篇文章主要介紹了AJAX Servlet實(shí)現(xiàn)數(shù)據(jù)異步交互的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
java開(kāi)發(fā)RocketMQ消息中間件原理基礎(chǔ)詳解
最近 RocketMQ 剛剛上生產(chǎn)環(huán)境,閑暇之時(shí)在這里做一些分享,主要目的是讓初學(xué)者能快速上手RocketMQ,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11

