java我这个字典的程序哪里错了?
答案:3 悬赏:0 手机版
解决时间 2021-04-16 10:07
- 提问者网友:风月客
- 2021-04-16 01:31
java我这个字典的程序哪里错了?
最佳答案
- 五星知识达人网友:上分大魔王
- 2021-04-16 01:44
已修改,复制粘贴即可
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class Zidian extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
JTextField txf = new JTextField (20);
JTextArea jt = new JTextArea ();
JButton btn1 = new JButton ("查询");
Zidian ()
{
setTitle ("Search");
setBounds (400, 300, 450, 350);
JPanel pnl4 = new JPanel ();
pnl4.setBorder (BorderFactory.createTitledBorder ("Search"));
pnl4.add (txf);
pnl4.add (btn1);
btn1.addActionListener (this);
JPanel pnl5 = new JPanel (new BorderLayout ());
pnl5.setBorder (BorderFactory.createTitledBorder ("Result"));
jt.setWrapStyleWord (true);
jt.setLineWrap (true);
pnl5.add (new JScrollPane (jt), BorderLayout.CENTER);
JPanel all = new JPanel ();
all.setLayout (new BorderLayout ());
all.add (pnl4, BorderLayout.NORTH);
all.add (pnl5, BorderLayout.CENTER);
add (all);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible (true);
}
public void actionPerformed ( ActionEvent e )
{
if (e.getSource () == btn1)
{
String str = txf.getText ();
String sql = "select * from chinese where charsound like '%" + str + "%'";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try
{
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/japan?user=root&password=123");
stmt = conn.createStatement ();
rs = stmt.executeQuery (sql);
jt.setText ("");
while (rs.next ())
{
jt.append (new String (rs.getString ("charname").getBytes ("iso-8859-1"), "gb2312") + " ");
}
}
catch (Exception eq)
{
eq.printStackTrace ();
}
finally
{
try
{
if (null != rs)
{
rs.close ();
}
if (null != stmt)
{
stmt.close ();
}
if (null != conn)
{
conn.close ();
}
}
catch (SQLException ignore)
{}
}
}
}
public static void main ( String[] args )
{
new Zidian ();
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class Zidian extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
JTextField txf = new JTextField (20);
JTextArea jt = new JTextArea ();
JButton btn1 = new JButton ("查询");
Zidian ()
{
setTitle ("Search");
setBounds (400, 300, 450, 350);
JPanel pnl4 = new JPanel ();
pnl4.setBorder (BorderFactory.createTitledBorder ("Search"));
pnl4.add (txf);
pnl4.add (btn1);
btn1.addActionListener (this);
JPanel pnl5 = new JPanel (new BorderLayout ());
pnl5.setBorder (BorderFactory.createTitledBorder ("Result"));
jt.setWrapStyleWord (true);
jt.setLineWrap (true);
pnl5.add (new JScrollPane (jt), BorderLayout.CENTER);
JPanel all = new JPanel ();
all.setLayout (new BorderLayout ());
all.add (pnl4, BorderLayout.NORTH);
all.add (pnl5, BorderLayout.CENTER);
add (all);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible (true);
}
public void actionPerformed ( ActionEvent e )
{
if (e.getSource () == btn1)
{
String str = txf.getText ();
String sql = "select * from chinese where charsound like '%" + str + "%'";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try
{
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/japan?user=root&password=123");
stmt = conn.createStatement ();
rs = stmt.executeQuery (sql);
jt.setText ("");
while (rs.next ())
{
jt.append (new String (rs.getString ("charname").getBytes ("iso-8859-1"), "gb2312") + " ");
}
}
catch (Exception eq)
{
eq.printStackTrace ();
}
finally
{
try
{
if (null != rs)
{
rs.close ();
}
if (null != stmt)
{
stmt.close ();
}
if (null != conn)
{
conn.close ();
}
}
catch (SQLException ignore)
{}
}
}
}
public static void main ( String[] args )
{
new Zidian ();
}
}
全部回答
- 1楼网友:深街酒徒
- 2021-04-16 03:54
你每个字符后面都加%是什么意思 这样能查找到?追问
就这里错了吗??
追答Mywindow win=new Mywindow(); ?根本没这个类你代码怎么运行的
就这里错了吗??
追答Mywindow win=new Mywindow(); ?根本没这个类你代码怎么运行的
- 2楼网友:轮獄道
- 2021-04-16 03:08
循环是for不是fot啊
for(int k=0;k 为什么要搞辣么多双引号?
sql="select*from chinese where charsound like"+str;追问
这2个我改了,又有新问题了
for(int k=0;k
sql="select*from chinese where charsound like"+str;追问
这2个我改了,又有新问题了
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯