java,,res=sql.executeQuery("select top 10 * from....在JFrame上,点击按钮查询,怎么写代码啊?
importjava.sql.*;publicclassTool2{//创建类staticConnectioncon;//声明Connection对象staticStat...
import java.sql.*;
public class Tool2 { // 创建类
static Connection con; // 声明Connection对象
static Statement sql; // 声明Statement对象
static ResultSet res; // 声明ResultSet对象
public Connection getConnection() { // 连接数据库方法
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:jtds:sqlserver://localhost:1433/baite2008", "sa", "0000");
} catch (Exception e) {
e.printStackTrace();
}
return con; // 返回Connection对象
}
public static void main(String[] args) { // 主方法
Tool2 c = new Tool2(); // 创建本类对象
con = c.getConnection(); // 与数据库建立连接
try {
sql=con.createStatement();
res=sql.executeQuery("select top 10 * from btoroom");
while(res.next()){
String id =res.getString("id");
String feileih=res.getString("feileih");
String isbn =res.getString("isbn");
String 书名=res.getString("bnamezw");
System.out.println("id:"+id);
System.out.println("分类号:"+feileih);
System.out.println("isbn:"+isbn);
System.out.println("书名:"+书名);
}
} catch (Exception e) {
e.printStackTrace();
}
}
} 展开
public class Tool2 { // 创建类
static Connection con; // 声明Connection对象
static Statement sql; // 声明Statement对象
static ResultSet res; // 声明ResultSet对象
public Connection getConnection() { // 连接数据库方法
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:jtds:sqlserver://localhost:1433/baite2008", "sa", "0000");
} catch (Exception e) {
e.printStackTrace();
}
return con; // 返回Connection对象
}
public static void main(String[] args) { // 主方法
Tool2 c = new Tool2(); // 创建本类对象
con = c.getConnection(); // 与数据库建立连接
try {
sql=con.createStatement();
res=sql.executeQuery("select top 10 * from btoroom");
while(res.next()){
String id =res.getString("id");
String feileih=res.getString("feileih");
String isbn =res.getString("isbn");
String 书名=res.getString("bnamezw");
System.out.println("id:"+id);
System.out.println("分类号:"+feileih);
System.out.println("isbn:"+isbn);
System.out.println("书名:"+书名);
}
} catch (Exception e) {
e.printStackTrace();
}
}
} 展开
2个回答
展开全部
按钮的话需要加入actionPerform,点击按钮执行代码,给些代码给你参考一下
public void init(){
columnNames=new Vector<Serializable>();
columnNames.add("ID");
columnNames.add("学号");
columnNames.add("姓名");
columnNames.add("性别");
columnNames.add("班别");
columnNames.add("专业");
columnNames.add("入校");
rowData=new Vector<Serializable>();
String driverClass="com.mysql.jdbc.Driver";
String jdbcUrl="jdbc:mysql:///students?useUnicode=true&characterEncoding=UTF8";
String username="";
String password="";
try{
Class.forName(driverClass);
conn=DriverManager.getConnection(jdbcUrl, username, password);
pstmt=conn.prepareStatement("SELECT * FROM `xuesheng`");
rs=pstmt.executeQuery();
while(rs.next()){
Vector<Object> hang=new Vector<Object>();
hang.add(Integer.parseInt(rs.getString(1)));
hang.add(Integer.parseInt(rs.getString(2)));
hang.add(rs.getString(3));
hang.add(rs.getString(4));
hang.add(rs.getString(5));
hang.add(rs.getString(6));
hang.add(rs.getString(7));
rowData.add(hang);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
展开全部
package com.swing;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
public class Login extends JFrame implements ActionListener {
/**
* @Fields serialVersionUID :
*/
private static final long serialVersionUID = 1L;
public JLabel j = null;
public JLabel j1 = null;
public JTextField jTextField = null;
public JTextField jTextField2 = null;
public JButton jButton=null;
public Login() {
//setLayout(new FlowLayout());
setLayout(new FlowLayout(FlowLayout.LEFT,20,40));
setFont(new Font("Helvetica", Font.PLAIN, 14));
j = new JLabel("登录名");
j1 = new JLabel("密码");
jTextField = new JTextField(10);
jTextField2 = new JTextField(10);
jButton=new JButton();
Dimension preferredSize = new Dimension(400,400);//设置尺寸
jButton.setPreferredSize(preferredSize );
/*jButton.setBounds(0, 0, 85, 82);
ImageIcon icon = new ImageIcon("E:\\workspace\\weixin-admin-pom\\src\\main\\java\\com\\swing\\dl.png");
Image temp = icon.getImage().getScaledInstance(jButton.getWidth(),
jButton.getHeight(), icon.getImage().SCALE_DEFAULT);
icon = new ImageIcon(temp);
jButton.setIcon(icon);*/
getContentPane().add(j);
getContentPane().add(jTextField);
getContentPane().add(j1);
getContentPane().add(jTextField2);
getContentPane().add(jButton);
jButton.addActionListener(this);
jButton.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
// CTRL+C按下
if ( e.getKeyCode()==KeyEvent.VK_ENTER) {
System.err.println("回车了");
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
}
public static void main(String[] args) {
Login window = new Login();
window.setTitle("登录");
// 该代码依据放置的组件设定窗口的大小使之正好能容纳你放置的所有组件
window.pack();
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null); // 让窗体居中显示
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==jButton) {
//查询调你写的操作数据库代码
// JOptionPane.showMessageDialog(this, "这是一个简单的消息框");
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询