急求java程序,现有访问数据库的程序,里面的sql是固定的,求java界面,可以输入sql语句,同时能够将查询

数据库的表很多,结构也不一样,应该怎么将字段结合到需要显示的表格中。十分感谢。... 数据库的表很多,结构也不一样,应该怎么将字段结合到需要显示的表格中。十分感谢。 展开
 我来答
1425077119
2011-07-30 · TA获得超过131个赞
知道答主
回答量:123
采纳率:0%
帮助的人:74.4万
展开全部
我用netbeans 写的,,在IE上可以运行结果,,,直接查询数据库内容,,加驱动mysql-connector-java<%@page contentType="text/html" pageEncoding="GBK"%>
<%@page language="java" import="java.util.*"%>
<%@page import="com.java.*" import="java.sql.*"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html public"-//w3c//dtd html 4.01 Transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>JSP 'showAll.jsp' startiong page</title>
</head>
<%
List<DeptVo> list=new ArrayList<DeptVo>();

try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("创建驱动成功!");
Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/testbank","root","1989");
System.out.println("连接数据库成功!");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from dept");
while(rs.next()){
DeptVo deptvo=new DeptVo();
deptvo.setId(rs.getString("id"));
deptvo.setAddress(rs.getString("address"));
deptvo.setNum(rs.getInt("num"));
deptvo.setName(rs.getString("name"));
deptvo.setD_id(rs.getInt("d_id"));
list.add(deptvo);
}
request.setAttribute("list",list);
}catch(Exception e){

}
%>

<body bgcolor="#00FFFF">
<table border="1" align="center" width="70%">
<tr>
<td>部门编号</td>
<td>部门地址</td>
<td>部门人数</td>
<td>部门名称</td>
<td>部门id</td>
</tr>
<c:forEach items="${list}" var="list">
<tr>
<td>${list.id}</td>
<td>${list.address}</td>
<td>${list.num}</td>
<td>${list.name}</td>
<td>${list.d_id}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.java;
public class DeptVo {
private String id;
private String address;
private int num;
private String name;
private int d_id;
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address=address;
}
public String getId(){
return id;
}
public void setId(String id){
this.id=id;
}
public int getNum(){
return num;
}
public void setNum(int num){
this.num=num;
}

public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getD_id(){
return d_id;
}
public void setD_id(int d_id){
this.d_id=d_id;
}

}
可以用eclipse,,
package com.cn.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

//import java.awt.List;

public class Query {
public List<UserVo> showUser(){
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
List<UserVo> list=new ArrayList<UserVo>();
try{
conn=JDBC_Connection.getConnection();
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from users");
while(rs.next()){
UserVo userVo=new UserVo();
userVo.setId(rs.getInt("id"));
userVo.setName(rs.getString("name"));
userVo.setAge(rs.getInt("age"));
userVo.setTel(rs.getString("tel"));
userVo.setAddress(rs.getString("address"));
list.add(userVo);

}
}catch(SQLException e){
e.printStackTrace();
}finally{
JDBC_Connection.free(rs, conn, stmt);
}
return list;
}
public static void main(String[] args) {
Query query=new Query();
List<UserVo> list=query.showUser();
if(list!=null){
System.out.print("id\t");
System.out.print("name\t");
System.out.print("age\t");
System.out.print("tel\t");
System.out.print("address\t");
System.out.println();
for(int i=0;i<list.size();i++){
System.out.print(list.get(i).getId()+"\t");
System.out.print(list.get(i).getName()+"\t");
System.out.print(list.get(i).getAge()+"\t");
System.out.print(list.get(i).getTel()+"\t ");
System.out.print(list.get(i).getAddress()+"\t");
System.out.println();
}

}

}

}
package com.cn.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.PreparedStatement;

public class QueryById {
public UserVo queryUserById(int id){
UserVo userVo=null;
Connection conn=null;
PreparedStatement psmt=null;
ResultSet rs=null;
try{
conn=JDBC_Connection.getConnection();
psmt=(PreparedStatement) conn.prepareStatement("select * from users where id=?");
psmt.setInt(1, id);
rs=psmt.executeQuery();
while(rs.next()){
userVo=new UserVo();
userVo.setId(rs.getInt("id"));
userVo.setName(rs.getString("name"));
userVo.setAge(rs.getInt("age"));
userVo.setTel(rs.getString("tel"));
userVo.setAddress(rs.getString("address"));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
JDBC_Connection.free(rs,conn,psmt);
}

return userVo;

}
public static void main(String[] args) {
QueryById byId=new QueryById();
int id=211;
UserVo vo=byId.queryUserById(id);
if(vo!=null){
System.out.print("id\t");
System.out.print("name\t");
System.out.print("age\t");
System.out.print("tel\t");
System.out.print("address\t");
System.out.println();
System.out.print(vo.getId()+"\t");
System.out.print(vo.getName()+"\t");
System.out.print(vo.getAge()+"\t");
System.out.print(vo.getTel()+"\t ");
System.out.print(vo.getAddress()+"\t");
System.out.println();
}else{
System.out.println("id为"+id+"的用户不存在");
}

}

}
package com.cn.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.PreparedStatement;

public class QueryById {
public UserVo queryUserById(int id){
UserVo userVo=null;
Connection conn=null;
PreparedStatement psmt=null;
ResultSet rs=null;
try{
conn=JDBC_Connection.getConnection();
psmt=(PreparedStatement) conn.prepareStatement("select * from users where id=?");
psmt.setInt(1, id);
rs=psmt.executeQuery();
while(rs.next()){
userVo=new UserVo();
userVo.setId(rs.getInt("id"));
userVo.setName(rs.getString("name"));
userVo.setAge(rs.getInt("age"));
userVo.setTel(rs.getString("tel"));
userVo.setAddress(rs.getString("address"));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
JDBC_Connection.free(rs,conn,psmt);
}

return userVo;

}
public static void main(String[] args) {
QueryById byId=new QueryById();
int id=211;
UserVo vo=byId.queryUserById(id);
if(vo!=null){
System.out.print("id\t");
System.out.print("name\t");
System.out.print("age\t");
System.out.print("tel\t");
System.out.print("address\t");
System.out.println();
System.out.print(vo.getId()+"\t");
System.out.print(vo.getName()+"\t");
System.out.print(vo.getAge()+"\t");
System.out.print(vo.getTel()+"\t ");
System.out.print(vo.getAddress()+"\t");
System.out.println();
}else{
System.out.println("id为"+id+"的用户不存在");
}

}

}
package com.cn.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.PreparedStatement;

public class QueryById {
public UserVo queryUserById(int id){
UserVo userVo=null;
Connection conn=null;
PreparedStatement psmt=null;
ResultSet rs=null;
try{
conn=JDBC_Connection.getConnection();
psmt=(PreparedStatement) conn.prepareStatement("select * from users where id=?");
psmt.setInt(1, id);
rs=psmt.executeQuery();
while(rs.next()){
userVo=new UserVo();
userVo.setId(rs.getInt("id"));
userVo.setName(rs.getString("name"));
userVo.setAge(rs.getInt("age"));
userVo.setTel(rs.getString("tel"));
userVo.setAddress(rs.getString("address"));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
JDBC_Connection.free(rs,conn,psmt);
}

return userVo;

}
public static void main(String[] args) {
QueryById byId=new QueryById();
int id=211;
UserVo vo=byId.queryUserById(id);
if(vo!=null){
System.out.print("id\t");
System.out.print("name\t");
System.out.print("age\t");
System.out.print("tel\t");
System.out.print("address\t");
System.out.println();
System.out.print(vo.getId()+"\t");
System.out.print(vo.getName()+"\t");
System.out.print(vo.getAge()+"\t");
System.out.print(vo.getTel()+"\t ");
System.out.print(vo.getAddress()+"\t");
System.out.println();
}else{
System.out.println("id为"+id+"的用户不存在");
}

}

}
package com.cn.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.PreparedStatement;

public class QueryById {
public UserVo queryUserById(int id){
UserVo userVo=null;
Connection conn=null;
PreparedStatement psmt=null;
ResultSet rs=null;
try{
conn=JDBC_Connection.getConnection();
psmt=(PreparedStatement) conn.prepareStatement("select * from users where id=?");
psmt.setInt(1, id);
rs=psmt.executeQuery();
while(rs.next()){
userVo=new UserVo();
userVo.setId(rs.getInt("id"));
userVo.setName(rs.getString("name"));
userVo.setAge(rs.getInt("age"));
userVo.setTel(rs.getString("tel"));
userVo.setAddress(rs.getString("address"));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
JDBC_Connection.free(rs,conn,psmt);
}

return userVo;

}
public static void main(String[] args) {
QueryById byId=new QueryById();
int id=211;
UserVo vo=byId.queryUserById(id);
if(vo!=null){
System.out.print("id\t");
System.out.print("name\t");
System.out.print("age\t");
System.out.print("tel\t");
System.out.print("address\t");
System.out.println();
System.out.print(vo.getId()+"\t");
System.out.print(vo.getName()+"\t");
System.out.print(vo.getAge()+"\t");
System.out.print(vo.getTel()+"\t ");
System.out.print(vo.getAddress()+"\t");
System.out.println();
}else{
System.out.println("id为"+id+"的用户不存在");
}

}

}
自己加2驱动把,,自己看吧,,
百度网友845f74e61
2011-08-01 · TA获得超过6929个赞
知道大有可为答主
回答量:4050
采纳率:50%
帮助的人:1621万
展开全部
这和一个客户端没什么区别了。
oracle上以前下过一下,你去找找看看
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
緾眠游戏
2011-07-30 · 超过33用户采纳过TA的回答
知道答主
回答量:153
采纳率:0%
帮助的人:72.2万
展开全部
能说清楚 点么?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式