189 8069 5689

综合查询Java代码的简单介绍

SSM框架,根据id查询一条数据的java代码怎么写

查询语句是要写在xml文件中的,如select * from table_name where id = #{id},#{id}表示取值。比如你在前台传来一个id,在后台接收到这个id,然后通过方法findById(String id)去查,此时#{id},取的就是这个id值

目前成都创新互联公司已为上千家的企业提供了网站建设、域名、网页空间、网站托管、服务器托管、企业网站设计、东川网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

java的代码分享网站有哪些?

1. java2s

这个网站非常好,分成三大类,分别是Example 、Products 、 Articles ,每个大类下又分别设许多小类,还有搜索功能,这样查找起来非常方便。。比如,如果要学习SWT/JFace,只要把Example下的SWT JFace Eclipse 研究一下也就可以了。另外,这个网站还有JavaScript DHTML 、 C# / C Sharp 、 C / ANSI-C 、 SQL / MySQL 等类。总之,非常好。

2. codeZoo

这是O'Reily旗下的,除了Java之外,还有Ruby、Python。

3. Java学习源代码检索系统

难得看见国产的,好歹也要支持一下,分类也算清楚。

4. Koders

是个综合查询的网站,不过它好像是从代码中查找关键词,包含的语言挺多的。

5. Resources for Java server-side developers

确切的说,它是一个资源收集的网站,代码查询并不多。不过它分类相当细,如Articles、Books、Examples、Extensions、Frameworks等类,你可以输入Spring或Hibernate作为关键词搜索一下看看。

-----

学生信息综合查询管理系统 JAVA程序编写

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("学号"));

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("专业"));

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,"数据已入库!","提示对话框",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("输入要查询的学号"));

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("专业"));

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("学号");

name=rs.getString("姓名");

sex=rs.getString("性别");

subject=rs.getString("专业");

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("没有该学生");}

}

}

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("输入要修改信息的学号"));

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("(新)专业"));

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("学号");

name=rs.getString("姓名");

sex=rs.getString("性别");

subject=rs.getString("专业");

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+", 专业="+s4+", 年级="+s5+", 出生="+s6+" WHERE 学号="+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("输入要删除的学号"));

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("专业"));

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,"确定要删除该学号及全部信息吗?","确定",JOptionPane.YES_NO_OPTION);

if(n==JOptionPane.YES_OPTION){

try{

String s1="'"+t1.getText().trim()+"'";

String temp="DELETE FROM jesse WHERE 学号="+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("学号");

name=rs.getString("姓名");

sex=rs.getString("性别");

subject=rs.getString("专业");

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("欢迎进入学生信息管理系统");

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("退出系统");

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("欢迎进入学生信息管理系统",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("欢迎进入学生信息管理系统",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);

}

});

}

}

java如何实现sql连接和查询的代码?

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 {

//数据库驱动对象

public static final String DRIVER="oracle.jdbc.driver.OracleDriver";

//数据库连接地址(数据库名)

public static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";

//登陆名

public static final String USER="FM";

//登陆密码

public static final String PWD="FM";

//创建数据库连接对象

private Connection con=null;

//创建数据库预编译对象

private PreparedStatement ps=null;

//创建结果集

private ResultSet rs=null;

//创建数据源对象

public static DataSource source=null;

//  //静态代码块  

//  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();  

//      }  

//  

//  

//  }

/**

* 获取数据库连接

*/

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;

}  

//  /**  

//   * 获取数据库连接  

//   */  

//  public Connection getCon(){  

//  

//      try {  

//          con=source.getConnection();  

//      } catch (SQLException e) {  

//          // TODO Auto-generated catch block  

//          e.printStackTrace();  

//      }  

//  

//      return con;  

//  }  

/**

* 关闭所有资源

*/

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数据库更新(增、删、改) 语句

* @param pras参数列表(可传,可不传,不传为NULL,以数组形式存在)

* @return 返回受影响都行数

*/

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数据库查询语句

* @param pras参数列表(可传,可不传,不传为NULL,以数组形式存在)

* @return 返回结果集

*/

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://jkwzsj.com/article/dosdije.html

其他资讯