我用下面的代码连接MySQL在localhost从Android系统.它仅显示catch部分中给出的操作.我不知道是否是连接问题.
package com.test1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Test1Activity extends Activity {
    /** Called when the activity is first created. */
    String str="new";
    static ResultSet rs;
    static PreparedStatement st;
    static Connection con;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final   TextView tv=(TextView)findViewById(R.id.user);
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
              con=DriverManager.getConnection("jdbc:mysql://10.0.2.2:8080/example","root","");
            st=con.prepareStatement("select * from country where id=1");
            rs=st.executeQuery();
             while(rs.next())
             {
             str=rs.getString(2);
             }
            tv.setText(str);
            setContentView(tv);
        }
        catch(Exception e)
        {
            tv.setText(str); …Run Code Online (Sandbox Code Playgroud) 我正在尝试用XML解析CDATA tpyes.代码运行正常,它将打印链接:在控制台中(大约50次,因为这是我有多少链接)但链接不会出现......它只是一个空白的控制台空间.我能错过什么?``
package Parse;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XMLParse {
  public static void main(String[] args) throws Exception {
    File file = new File("c:test/returnfeed.xml");
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(file);
    NodeList nodes = doc.getElementsByTagName("video");
    for (int i = 0; i < nodes.getLength(); i++) {
      Element element = (Element) nodes.item(i);
      NodeList title = element.getElementsByTagName("videoURL");
      Element line = (Element) title.item(0);
      System.out.println("Links: " + getCharacterDataFromElement(line));
    }
  }
  public …Run Code Online (Sandbox Code Playgroud) 大家好我想在html页面上显示我的数据库表的全部内容.我正在尝试从数据库中获取记录并存储ArrayList但是当我在html页面上返回数组列表时,它只重复显示最后一条记录作为我的数据库表的计数.以下是代码:             
public ArrayList<CustomerDTO> getAllCustomers() 
{
    ArrayList<CustomerDTO> customers = new ArrayList<CustomerDTO>();
    CustomerDTO customer = null;
    Connection c;
    try {
        c = openConnection();
        Statement statement = c.createStatement();
        String s = "SELECT * FROM customer";
        ResultSet rs = statement.executeQuery(s);
        int g =0;
        while (rs.next()) {
            customer.setId(rs.getInt("id"));
            customer.setName(rs.getString("name"));
            customer.setAddress(rs.getString("address"));
            customer.setPhone(rs.getString("phone"));
            customer.setEmail(rs.getString("email"));
            customer.setBountPoints(rs.getInt("bonuspoint"));
            customer.setTotalsale(rs.getInt("totalsale"));
            customers.add(customer);
        }
        rs.close();
    } catch (Exception e) {
        System.out.println(e);
    }
    return customers;
}
Run Code Online (Sandbox Code Playgroud) 例如,房地产建造者正在建造一个有许多单位的公寓.除卧室外,公寓的所有客房均采用相同的设计.卧室设计留给拥有公寓的人们,即; 床的房间可以是不同的设计,适合不同的公寓.
我可以通过以下abstract类来实现这一点:
public abstract class Flat
{
    //some properties
    public void livingRoom(){
       //some code
    }
    public void kitchen(){
       //some code
    }
    public abstract void bedRoom();
    }
}
Run Code Online (Sandbox Code Playgroud)
一implementation类是如下:
public class Flat101 extends Flat
{
    public void bedRoom() {
        System.out.println("This flat has a customized bedroom");
   }        
}
Run Code Online (Sandbox Code Playgroud)
或者,我可以使用interface而不是abstract类来实现相同的目的,如下所示:
class Flat
{
  public void livingRoom(){ 
       System.out.println("This flat has a living room");
  }
  public void kitchen(){
     System.out.println("This flat has a kitchen");
  } 
}
interface …Run Code Online (Sandbox Code Playgroud) 我有一个用于显示文本文件的 JTextPane。文字显示如下
Line 1
Line 2
Line 3
我想要得到的是我的插入符当前所在行的起始索引。JTextPane 中有一个简单的方法可以帮助我实现这一目标吗?
所以我要求基于JComboBox中项目的选择,我需要向用户显示选择确认对话框.我做的是添加一个ItemListener并基于某个逻辑,我弹出这个对话框.
我面临的相当棘手的问题是对话框首先弹出(即使ComboBox项目选择打开),我要点击两次以确认我的选择.第一个是关闭ComboBox弹出窗口,第二个是确认对话框中的实际弹出窗口.
这是SSCCE突出我的问题:
import java.awt.event.ItemEvent;
import javax.swing.JOptionPane;
public class TestFrame extends javax.swing.JFrame {
    /**
     * Creates new form TestFrame
     */
    public TestFrame() {
        initComponents();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        selectCombo = new javax.swing.JComboBox(); …Run Code Online (Sandbox Code Playgroud) 嗨,我想获得数组中最低的3个元素.最低的是指最小值.我不能使用collections.Sort方法,因为我需要知道元素的索引.因此我使用以下代码来获得最低,但我需要知道如何获得最低3.
int minimum = grades[1];
int index = 1;
for(i=1; i<= numberOfStudents; i++){
    if (grades[i]<minimum){
        minimum = grades[i];
        index = i;
    }
}
Run Code Online (Sandbox Code Playgroud) 我试图从CSV文件中读取数据并将其显示在JTable上,但我有一些问题.我是菜鸟,请耐心等待.我查看并合并了几个来源的示例代码,但无济于事.该表显示但是它是空白的.我知道我正在阅读数据,因为我可以打印它.我怀疑我的ModelTable设置有问题.任何帮助将不胜感激.
package t1data;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.*;
import javax.swing.border.EmptyBorder;
import java.io.*;
import javax.swing.table.*;
public class T1Data extends JPanel implements ActionListener {
public T1Data() {
        super(new BorderLayout(3,3));
        JTable Table = new JTable(new MyModel());        
        Table.setPreferredScrollableViewportSize(new Dimension(700, 70));
        Table.setFillsViewportHeight(true);
        JPanel ButtonOpen = new JPanel( new FlowLayout(FlowLayout.CENTER) );
        JButton OpenFile = new JButton("Open");
        JButton SaveFile = new JButton("Save");
        ButtonOpen.add(OpenFile);
        add(ButtonOpen, BorderLayout.SOUTH);
        OpenFile.addActionListener(this);
        SaveFile.addActionListener(this);
        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(Table); …Run Code Online (Sandbox Code Playgroud) 我正在使用JFreeChart创建一个XYBarChart并向其添加多个系列.目前,对于给定的x值和来自该系列的不同Y值,它们全部堆叠在彼此之上.
是否可以将每个系列显示为给定x值的不同栏?
编辑:我发布了我的代码的相关部分,用于创建图表供您参考.
请注意,我无法使用CategoryDataset,因为这不会在域轴上提供缩放功能.这是我实施的基本要求.
XYSeriesCollection intervalXYDataSet  = new XYSeriesCollection();
int countPlotPoints = populateBandData(intervalXYDataSet, optionList); //optionList is a HashMap<Integer,ArrayList<Integer>> where key = seriesKey, and ArrayList<Integer> builds up the value for each series
if (countPlotPoints == 0) {
    print("No options selected.\n");
    JOptionPane.showMessageDialog(this, "No Plot Points Were Selected!", "Warning", JOptionPane.WARNING_MESSAGE);
    return;
}
/**
 * Chart Creation Section
 */
JFreeChart chart = ChartFactory.createXYBarChart(tabTitle, "Frequency Bands", false, "rxlev", intervalXYDataSet, PlotOrientation.VERTICAL, true, true, false);
XYPlot plot = (XYPlot) chart.getPlot();
plot.getRangeAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
plot.getDomainAxis().setAutoRange(true);
// …Run Code Online (Sandbox Code Playgroud) 如何在print语句中编写if语句?
public boolean checkEmpty()
{
    if(array.isEmpty)
    {
        Sytem.out.println("The List is empty");
    }
    else
    {
        System.out.println("The list has: " +  if(array.size() > 1)) {"Items"} + else {"item"} );
    }
}
Run Code Online (Sandbox Code Playgroud) java ×9
swing ×3
jdbc ×2
android ×1
arrays ×1
bar-chart ×1
boolean ×1
file-io ×1
if-statement ×1
interface ×1
itemlistener ×1
jcombobox ×1
jfreechart ×1
joptionpane ×1
jtable ×1
jtextpane ×1
math ×1
minimum ×1
mysql ×1
oop ×1
parsing ×1
printing ×1
system.out ×1
xml ×1