查詢語句是要寫在xml文件中的,如select * from table_name where id = #{id},#{id}表示取值。比如你在前臺傳來一個id,在后臺接收到這個id,然后通過方法findById(String id)去查,此時#{id},取的就是這個id值
目前成都創(chuàng)新互聯(lián)公司已為上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計、東川網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
1. java2s
這個網(wǎng)站非常好,分成三大類,分別是Example 、Products 、 Articles ,每個大類下又分別設(shè)許多小類,還有搜索功能,這樣查找起來非常方便。。比如,如果要學(xué)習(xí)SWT/JFace,只要把Example下的SWT JFace Eclipse 研究一下也就可以了。另外,這個網(wǎng)站還有JavaScript DHTML 、 C# / C Sharp 、 C / ANSI-C 、 SQL / MySQL 等類??傊?,非常好。
2. codeZoo
這是O'Reily旗下的,除了Java之外,還有Ruby、Python。
3. Java學(xué)習(xí)源代碼檢索系統(tǒng)
難得看見國產(chǎn)的,好歹也要支持一下,分類也算清楚。
4. Koders
是個綜合查詢的網(wǎng)站,不過它好像是從代碼中查找關(guān)鍵詞,包含的語言挺多的。
5. Resources for Java server-side developers
確切的說,它是一個資源收集的網(wǎng)站,代碼查詢并不多。不過它分類相當(dāng)細(xì),如Articles、Books、Examples、Extensions、Frameworks等類,你可以輸入Spring或Hibernate作為關(guān)鍵詞搜索一下看看。
-----
package test;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.JOptionPane;
class Add extends Panel implements ActionListener{
Connection con;
Statement sql;
Button b1,b2;
TextField tf1,tf2,tf3,tf4,tf5,tf6;
Box baseBox,bv1,bv2;
Add(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try{
con=DriverManager.getConnection("jdbc:odbc:data","","");
sql=con.createStatement();
}
catch(SQLException ee){}
setLayout(new BorderLayout());
Panel p1=new Panel();
Panel p2=new Panel();
tf1=new TextField(16);
tf2=new TextField(16);
tf3=new TextField(16);
tf4=new TextField(16);
tf5=new TextField(16);
tf6=new TextField(16);
b1=new Button("錄入");
b1.setBackground(Color.green);
b2=new Button("重置");
b2.setBackground(Color.green);
b1.addActionListener(this);
b2.addActionListener(this);
p1.add(b1);
p1.add(b2);
bv1=Box.createVerticalBox();
bv1.add(new Label("學(xué)號"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("姓名"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("性別"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("專業(yè)"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("年級"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("出生"));
bv1.add(Box.createVerticalStrut(8));
bv2=Box.createVerticalBox();
bv2.add(tf1);
bv2.add(Box.createVerticalStrut(8));
bv2.add(tf2);
bv2.add(Box.createVerticalStrut(8));
bv2.add(tf3);
bv2.add(Box.createVerticalStrut(8));
bv2.add(tf4);
bv2.add(Box.createVerticalStrut(8));
bv2.add(tf5);
bv2.add(Box.createVerticalStrut(8));
bv2.add(tf6);
bv2.add(Box.createVerticalStrut(8));
baseBox=Box.createHorizontalBox();
baseBox.add(bv1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(bv2);
p2.add(baseBox);
add(p1,"South");
add(p2,"Center");
setSize(350,300);
setBackground(Color.pink);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
try{ insert();}
catch(SQLException ee){}
JOptionPane.showMessageDialog(this,"數(shù)據(jù)已入庫!","提示對話框",JOptionPane.INFORMATION_MESSAGE);
}
else if(e.getSource()==b2){
tf1.setText(" ");
tf2.setText(" ");
tf3.setText(" ");
tf4.setText(" ");
tf5.setText(" ");
tf6.setText(" ");
}
}
public void insert() throws SQLException{
String s1="'"+tf1.getText().trim()+"'";
String s2="'"+tf2.getText().trim()+"'";
String s3="'"+tf3.getText().trim()+"'";
String s4="'"+tf4.getText().trim()+"'";
String s5="'"+tf5.getText().trim()+"'";
String s6="'"+tf6.getText().trim()+"'";
String temp="INSERT INTO jesse VALUES ("+s1+","+s2+","+s3+","+s4+","+s5+","+s6+")";
con=DriverManager.getConnection("jdbc:odbc:data","","");
sql.executeQuery(temp);
con.close();
}
}
class Query extends Panel implements ActionListener{
Connection con;
Statement sql;
TextField t1,t2,t3,t4,t5,t6;
Button b;
Box baseBox,bv1,bv2;
int flag=0;
Query(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try{
con=DriverManager.getConnection("jdbc:odbc:data","","");
sql=con.createStatement();
}
catch(SQLException ee){}
setLayout(new BorderLayout());
b=new Button("查詢");
b.setBackground(Color.orange);
b.addActionListener(this);
t1=new TextField(8);
t2=new TextField(16);
t3=new TextField(16);
t4=new TextField(16);
t5=new TextField(16);
t6=new TextField(16);
t2.setEditable(false);
t3.setEditable(false);
t4.setEditable(false);
t5.setEditable(false);
t6.setEditable(false);
Panel p1=new Panel(),p2=new Panel();
p1.add(new Label("輸入要查詢的學(xué)號"));
p1.add(t1);
p1.add(b);
bv1=Box.createVerticalBox();
bv1.add(new Label("姓名"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("性別"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("專業(yè)"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("年級"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("出生"));
bv1.add(Box.createVerticalStrut(8));
bv2=Box.createVerticalBox();
bv2.add(t2);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t3);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t4);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t5);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t6);
bv2.add(Box.createVerticalStrut(8));
baseBox=Box.createHorizontalBox();
baseBox.add(bv1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(bv2);
p2.add(baseBox);
add(p1,"North");
add(p2,"Center");
setSize(300,250);
setBackground(Color.red);
}
public void actionPerformed(ActionEvent e){
flag=0;
try{query();}
catch(SQLException ee){}
}
public void query() throws SQLException{
String num,name,sex,subject,grade,born;
con=DriverManager.getConnection("jdbc:odbc:data","","");
ResultSet rs=sql.executeQuery("SELECT * FROM jesse ");
while(rs.next()){
num=rs.getString("學(xué)號");
name=rs.getString("姓名");
sex=rs.getString("性別");
subject=rs.getString("專業(yè)");
grade=rs.getString("年級");
born=rs.getString("出生");
if(num.equals(t1.getText().trim())){
t2.setText(name);
t3.setText(sex);
t4.setText(subject);
t5.setText(grade);
t6.setText(born);
flag=1;
break;
}
}
con.close();
if(flag==0){t1.setText("沒有該學(xué)生");}
}
}
class Update extends Panel implements ActionListener{
Connection con;
Statement sql;
Button b1,b2,b3;
Box baseBox,bv1,bv2;
TextField t1,t2,t3,t4,t5,t6;
Update(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try{
con=DriverManager.getConnection("jdbc:odbc:data","","");
sql=con.createStatement();
}
catch(SQLException ee){}
setLayout(new BorderLayout());
b1=new Button("開始修改");
b1.setBackground(Color.green);
b2=new Button("錄入修改");
b2.setBackground(Color.yellow);
b3=new Button("重置");
b3.setBackground(Color.yellow);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
t1=new TextField(8);
t2=new TextField(16);
t3=new TextField(16);
t4=new TextField(16);
t5=new TextField(16);
t6=new TextField(16);
Panel p1=new Panel(),p2=new Panel(),p3=new Panel();
p1.add(new Label("輸入要修改信息的學(xué)號"));
p1.add(t1);
p1.add(b1);
bv1=Box.createVerticalBox();
bv1.add(new Label("(新)姓名"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("(新)性別"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("(新)專業(yè)"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("(新)年級"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("(新)出生"));
bv1.add(Box.createVerticalStrut(8));
bv2=Box.createVerticalBox();
bv2.add(t2);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t3);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t4);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t5);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t6);
bv2.add(Box.createVerticalStrut(8));
baseBox=Box.createHorizontalBox();
baseBox.add(bv1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(bv2);
p2.add(baseBox);
p3.add(b2);
p3.add(b3);
add(p1,"North");
add(p2,"Center");
add(p3,"South");
setSize(300,250);
setBackground(Color.cyan);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
try{
String num,name,sex,subject,grade,born;
con=DriverManager.getConnection("jdbc:odbc:data","","");
ResultSet rs=sql.executeQuery("SELECT * FROM jesse ");
while(rs.next()){
num=rs.getString("學(xué)號");
name=rs.getString("姓名");
sex=rs.getString("性別");
subject=rs.getString("專業(yè)");
grade=rs.getString("年級");
born=rs.getString("出生");
if(num.equals(t1.getText().trim())){
t2.setText(name);
t3.setText(sex);
t4.setText(subject);
t5.setText(grade);
t6.setText(born);
break;
}
}
con.close();
}
catch(SQLException ee){}
}
if(e.getSource()==b2){
try{update();}
catch(SQLException ee){}
}
if(e.getSource()==b3){
t2.setText(" ");
t3.setText(" ");
t4.setText(" ");
t5.setText(" ");
t6.setText(" ");
}
}
public void update() throws SQLException{
String s1="'"+t1.getText().trim()+"'";
String s2="'"+t2.getText().trim()+"'";
String s3="'"+t3.getText().trim()+"'";
String s4="'"+t4.getText().trim()+"'";
String s5="'"+t5.getText().trim()+"'";
String s6="'"+t6.getText().trim()+"'";
String temp="UPDATE jesse SET 姓名 ="+s2+", 性別="+s3+", 專業(yè)="+s4+", 年級="+s5+", 出生="+s6+" WHERE 學(xué)號="+s1;
con=DriverManager.getConnection("jdbc:odbc:data","","");
sql.executeQuery(temp);
con.close();
}
}
class Delete extends Panel implements ActionListener{
Connection con;
Statement sql;
TextField t1,t2,t3,t4,t5,t6;
Button b;
Box baseBox,bv1,bv2;
Delete(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try{
con=DriverManager.getConnection("jdbc:odbc:data","","");
sql=con.createStatement();
}
catch(SQLException ee){}
setLayout(new BorderLayout());
b=new Button("刪除");
b.setBackground(Color.cyan);
b.addActionListener(this);
t1=new TextField(8);
t1.addActionListener(this);
t2=new TextField(16);
t3=new TextField(16);
t4=new TextField(16);
t5=new TextField(16);
t6=new TextField(16);
t2.setEditable(false);
t3.setEditable(false);
t4.setEditable(false);
t5.setEditable(false);
t6.setEditable(false);
Panel p1=new Panel(),p2=new Panel();
p1.add(new Label("輸入要刪除的學(xué)號"));
p1.add(t1);
p1.add(b);
bv1=Box.createVerticalBox();
bv1.add(new Label("姓名"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("性別"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("專業(yè)"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("年級"));
bv1.add(Box.createVerticalStrut(8));
bv1.add(new Label("出生"));
bv1.add(Box.createVerticalStrut(8));
bv2=Box.createVerticalBox();
bv2.add(t2);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t3);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t4);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t5);
bv2.add(Box.createVerticalStrut(8));
bv2.add(t6);
bv2.add(Box.createVerticalStrut(8));
baseBox=Box.createHorizontalBox();
baseBox.add(bv1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(bv2);
p2.add(baseBox);
add(p1,"North");
add(p2,"Center");
setSize(300,250);
setBackground(Color.green);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==t1){
try{delete();}
catch(SQLException ee){}
}
else if(e.getSource()==b){
int n=JOptionPane.showConfirmDialog(this,"確定要刪除該學(xué)號及全部信息嗎?","確定",JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION){
try{
String s1="'"+t1.getText().trim()+"'";
String temp="DELETE FROM jesse WHERE 學(xué)號="+s1;
con=DriverManager.getConnection("jdbc:odbc:data","","");
sql.executeUpdate(temp);
con.close();
}
catch(SQLException ee){}
}
else if(n==JOptionPane.NO_OPTION){}
}
}
public void delete() throws SQLException{
String num,name,sex,subject,grade,born;
con=DriverManager.getConnection("jdbc:odbc:data","","");
ResultSet rs=sql.executeQuery("SELECT * FROM jesse ");
while(rs.next()){
num=rs.getString("學(xué)號");
name=rs.getString("姓名");
sex=rs.getString("性別");
subject=rs.getString("專業(yè)");
grade=rs.getString("年級");
born=rs.getString("出生");
if(num.equals(t1.getText().trim())){
t2.setText(name);
t3.setText(sex);
t4.setText(subject);
t5.setText(grade);
t6.setText(born);
break;
}
}
con.close();
}
}
public class tyj extends Frame implements ActionListener{
MenuBar bar=null;
Menu menu1,menu2,menu3,menu4,menu5;
MenuItem item1,item2,item3,item4,item5;
Add zengjia;
Query chaxun;
Update gengxin;
Delete shanchu;
tyj(){
super("歡迎進入學(xué)生信息管理系統(tǒng)");
zengjia=new Add();
chaxun=new Query();
gengxin=new Update();
shanchu=new Delete();
bar=new MenuBar();
menu1=new Menu("信息錄入");
menu2=new Menu("信息查詢");
menu3=new Menu("信息更新");
menu4=new Menu("信息刪除");
menu5=new Menu("退出系統(tǒng)");
item1=new MenuItem("錄入");
item2=new MenuItem("查詢");
item3=new MenuItem("更新");
item4=new MenuItem("刪除");
item5=new MenuItem("退出");
menu1.add(item1);
menu2.add(item2);
menu3.add(item3);
menu4.add(item4);
menu5.add(item5);
bar.add(menu1);
bar.add(menu2);
bar.add(menu3);
bar.add(menu4);
bar.add(menu5);
setMenuBar(bar);
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
item4.addActionListener(this);
item5.addActionListener(this);
Label label=new Label("歡迎進入學(xué)生信息管理系統(tǒng)",Label.CENTER);
String s=" ";
Font f=new Font(s,Font.BOLD,22);
label.setFont(f);
label.setBackground(Color.GREEN);
label.setForeground(Color.BLUE);
add(label,"Center");
setVisible(true);
setSize(320,300);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==item1){
removeAll();
add(zengjia,"Center");
validate();
}
if(e.getSource()==item2){
removeAll();
add(chaxun,"Center");
validate();
}
if(e.getSource()==item3){
removeAll();
add(gengxin,"Center");
validate();
}
if(e.getSource()==item4){
removeAll();
add(shanchu,"Center");
validate();
}
if(e.getSource()==item5){
removeAll();
setBackground(Color.GREEN);
Label label=new Label("歡迎進入學(xué)生信息管理系統(tǒng)",Label.CENTER);
String s=" ";
Font f=new Font(s,Font.BOLD,22);
label.setFont(f);
label.setForeground(Color.BLUE);
add(label,"Center");
validate();
}
}
public static void main(String[] args)
{
tyj jesse=new tyj();
jesse.setVisible(true);
jesse.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
import java.sql.Connection。
import java.sql.DriverManager; ?
import java.sql.PreparedStatement; ?
import java.sql.ResultSet; ?
import java.sql.SQLException;
import javax.naming.Context; ?
import javax.naming.InitialContext; ?
import javax.naming.NamingException; ?
import javax.sql.DataSource;
public class DBCon {
//數(shù)據(jù)庫驅(qū)動對象
public static final String DRIVER="oracle.jdbc.driver.OracleDriver";
//數(shù)據(jù)庫連接地址(數(shù)據(jù)庫名)
public static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
//登陸名
public static final String USER="FM";
//登陸密碼
public static final String PWD="FM";
//創(chuàng)建數(shù)據(jù)庫連接對象
private Connection con=null;
//創(chuàng)建數(shù)據(jù)庫預(yù)編譯對象
private PreparedStatement ps=null;
//創(chuàng)建結(jié)果集
private ResultSet rs=null;
//創(chuàng)建數(shù)據(jù)源對象
public static DataSource source=null;
// ?//靜態(tài)代碼塊 ?
// ?static{ ?
// ?
// ? ? ?//初始化配置文件context ?
// ? ? ?try { ?
// ? ? ? ? ?Context context=new InitialContext(); ?
// ? ? ? ? ?source=(DataSource)context.lookup("java:comp/env/jdbc/webmessage"); ?
// ? ? ?} catch (Exception e) { ?
// ? ? ? ? ?// TODO Auto-generated catch block ?
// ? ? ? ? ?e.printStackTrace(); ?
// ? ? ?} ?
// ?
// ?
// ?}
/**
* 獲取數(shù)據(jù)庫連接
*/
public Connection getCon(){
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con=DriverManager.getConnection(URL,USER,PWD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
} ?
// ?/** ?
// ? * 獲取數(shù)據(jù)庫連接 ?
// ? */ ?
// ?public Connection getCon(){ ?
// ?
// ? ? ?try { ?
// ? ? ? ? ?con=source.getConnection(); ?
// ? ? ?} catch (SQLException e) { ?
// ? ? ? ? ?// TODO Auto-generated catch block ?
// ? ? ? ? ?e.printStackTrace(); ?
// ? ? ?} ?
// ?
// ? ? ?return con; ?
// ?} ?
/**
* 關(guān)閉所有資源
*/
public void closeAll(){
if(rs!=null)
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(ps!=null)
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(con!=null)
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} ?
}
/**
* @param sql數(shù)據(jù)庫更新(增、刪、改) 語句
* @param pras參數(shù)列表(可傳,可不傳,不傳為NULL,以數(shù)組形式存在)
* @return 返回受影響都行數(shù)
*/
public int update(String sql,String... pras){
int resu=0;
con=getCon();
try {
ps=con.prepareStatement(sql);
for(int i=0;ipras.length;i++){
ps.setString(i+1,pras[i]);
}
resu=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
closeAll();
}
return resu;
}
/**
* @param sql數(shù)據(jù)庫查詢語句
* @param pras參數(shù)列表(可傳,可不傳,不傳為NULL,以數(shù)組形式存在)
* @return 返回結(jié)果集
*/
public ResultSet query(String sql,String... pras){
con=getCon();
try {
ps=con.prepareStatement(sql);
if(pras!=null)
for(int i=0;ipras.length;i++){
ps.setString(i+1, pras[i]);
}
rs=ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
} ?
}
本文名稱:綜合查詢Java代碼的簡單介紹
分享鏈接:http://jinyejixie.com/article24/dosdije.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、關(guān)鍵詞優(yōu)化、網(wǎng)站建設(shè)、、品牌網(wǎng)站設(shè)計、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)