当前位置: 技术问答>java相关
请教 mysql jdbc select 中文问题?
来源: 互联网 发布时间:2015-09-03
本文导语: SELECT * FROM table where id = "中文" 显示无纪录! 在表table中有 id 为 中文 的纪录,但总是选取不到! jdbc:mysql://localhost/test?user=root&password=pass&useUnicode=true&characterEncoding=GB2312 已经使用!对数据库增删改均没有问题! 源...
SELECT * FROM table where id = "中文"
显示无纪录!
在表table中有 id 为 中文 的纪录,但总是选取不到!
jdbc:mysql://localhost/test?user=root&password=pass&useUnicode=true&characterEncoding=GB2312
已经使用!对数据库增删改均没有问题!
源代码:
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class inensshow extends JFrame {
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private ResultSetMetaData rsMetaData;
private JTable table;
private JTextArea inputQuery;
private JButton submitQuery;
public inensshow()
{
super( "输入SQL语句,按查询按钮查看结果。" );
try {
Class.forName( "org.gjt.mm.mysql.Driver" );
connection = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=pass&useUnicode=true&characterEncoding=GB2312");
}
catch ( ClassNotFoundException cnfex ) {
System.err.println(
"装载 JDBC 驱动程序失败。" );
cnfex.printStackTrace();
System.exit( 1 );
}
catch ( SQLException sqlex ) {
System.err.println( "无法连接数据库" );
sqlex.printStackTrace();
System.exit( 1 );
}
String test="SELECT * FROM java";
inputQuery = new JTextArea( test, 4, 30 );
submitQuery = new JButton( "查询" );
submitQuery.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
getTable();
}
}
);
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
topPanel.add( new JScrollPane( inputQuery), BorderLayout.CENTER );
topPanel.add( submitQuery, BorderLayout.SOUTH );
table = new JTable();
Container c = getContentPane();
c.setLayout( new BorderLayout() );
c.add( topPanel, BorderLayout.NORTH );
c.add( table, BorderLayout.CENTER );
getTable();
setSize( 500, 300 );
show();
}
private void getTable()
{
try {
String query = inputQuery.getText();
statement = connection.createStatement();
resultSet = statement.executeQuery( query );
displayResultSet( resultSet );
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
private void displayResultSet( ResultSet rs )
throws SQLException
{
boolean moreRecords = rs.next();
if ( ! moreRecords ) {
JOptionPane.showMessageDialog( this,
"结果集中无记录" );
setTitle( "无记录显示" );
return;
}
Vector columnHeads = new Vector();
Vector rows = new Vector();
try {
ResultSetMetaData rsmd = rs.getMetaData();
for ( int i = 1; i
显示无纪录!
在表table中有 id 为 中文 的纪录,但总是选取不到!
jdbc:mysql://localhost/test?user=root&password=pass&useUnicode=true&characterEncoding=GB2312
已经使用!对数据库增删改均没有问题!
源代码:
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class inensshow extends JFrame {
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private ResultSetMetaData rsMetaData;
private JTable table;
private JTextArea inputQuery;
private JButton submitQuery;
public inensshow()
{
super( "输入SQL语句,按查询按钮查看结果。" );
try {
Class.forName( "org.gjt.mm.mysql.Driver" );
connection = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=pass&useUnicode=true&characterEncoding=GB2312");
}
catch ( ClassNotFoundException cnfex ) {
System.err.println(
"装载 JDBC 驱动程序失败。" );
cnfex.printStackTrace();
System.exit( 1 );
}
catch ( SQLException sqlex ) {
System.err.println( "无法连接数据库" );
sqlex.printStackTrace();
System.exit( 1 );
}
String test="SELECT * FROM java";
inputQuery = new JTextArea( test, 4, 30 );
submitQuery = new JButton( "查询" );
submitQuery.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
getTable();
}
}
);
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
topPanel.add( new JScrollPane( inputQuery), BorderLayout.CENTER );
topPanel.add( submitQuery, BorderLayout.SOUTH );
table = new JTable();
Container c = getContentPane();
c.setLayout( new BorderLayout() );
c.add( topPanel, BorderLayout.NORTH );
c.add( table, BorderLayout.CENTER );
getTable();
setSize( 500, 300 );
show();
}
private void getTable()
{
try {
String query = inputQuery.getText();
statement = connection.createStatement();
resultSet = statement.executeQuery( query );
displayResultSet( resultSet );
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
private void displayResultSet( ResultSet rs )
throws SQLException
{
boolean moreRecords = rs.next();
if ( ! moreRecords ) {
JOptionPane.showMessageDialog( this,
"结果集中无记录" );
setTitle( "无记录显示" );
return;
}
Vector columnHeads = new Vector();
Vector rows = new Vector();
try {
ResultSetMetaData rsmd = rs.getMetaData();
for ( int i = 1; i