日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java数据库查询简介

發(fā)布時間:2025/3/15 java 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java数据库查询简介 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

利用Connection對象的createStatement方法建立Statement對象,利用Statement對象的executeQuery()方法執(zhí)行SQL查詢語句進行查詢,返回結(jié)果集,再形如getXXX()的方法從結(jié)果集中讀取數(shù)據(jù)。經(jīng)過這樣的一系列步驟就能實現(xiàn)對數(shù)據(jù)庫的查詢。

【例 13-9】Java應(yīng)用程序訪問數(shù)據(jù)庫。應(yīng)用程序打開考生信息表ksInfo,從中取出考生的各項信息。設(shè)考生信息數(shù)據(jù)庫的結(jié)構(gòu)如下:

類型字符串字符串整數(shù)字符串字符串
字段名考號姓名成績地址簡歷

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ū)動程序
}
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[] ={“考號”, “姓名”, “成績”, “地址”, “簡歷”};
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(“前一個”);
JButton next = new JButton(“后一個”);
JButton first = new JButton(“第一個”);
JButton last = new JButton(“最后一個”);
Statement sql; //SQL語句對象
ResultSet rs; //存放查詢結(jié)果對象
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,
“請輸入考生考號”, “輸入考號對話框”, 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(“考號”)); txtName.setText(rs.getString(“姓名”)); txtScore.setText(rs.getString(“成績”));
txtAddr.setText(rs.getString(“地址”)); txtResume.setText(rs.getString(“簡歷”));
}
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ù)庫連接失敗!”);
System.exit ( – 1);
}
new Example10_9(connect); //創(chuàng)建對象
}
}

1.IP地址和InetAddress類

2.套接字(Socket)
3.數(shù)據(jù)庫連接
4.幾個重要的類和接口
5.數(shù)據(jù)庫查詢
6.數(shù)據(jù)庫更新
7.插入記錄
8.修改記錄
9.刪除記錄

總結(jié)

以上是生活随笔為你收集整理的Java数据库查询简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。