日期:2014-05-20  浏览次数:20726 次

java操作数据库出现乱码问题?
如题
代码如下


private   void   LoginInsert()   {

sqlname   =   "root ";
sqlpass   =   "zhangyang ";
String   url   =   "jdbc:mysql://localhost:3306/lucky ";
//String   url= "jdbc:mysql://localhost:3306/lucky?useUncode=true&characterEncoding=GBK ";

try   {
Class.forName( "com.mysql.jdbc.Driver ");
con   =   DriverManager.getConnection(url,   sqlname,   sqlpass);

}   catch   (ClassNotFoundException   e)   {
System.out.println( "装载驱动程序失败 ");
e.printStackTrace();
System.exit(1);
}   catch   (SQLException   e)   {
System.err.println( "无法连接数据库 ");
e.getMessage();
System.exit(1);
}

try   {
String   strsql;

strsql   =   "select   *   from   user   where   (name= ' "   +   newname   +   " ') ";
stmt   =   con.createStatement();   //   创建一个执行的用于执行静态   SQL   语句并返回它所生成结果的对象。
rs   =   stmt.executeQuery(strsql);

boolean   flags   =   rs.next();
System.out.println(flags);

if   (flags)   {
JOptionPane.showMessageDialog(this,   "用户名已存在 ");
username.setText( " ");
pw1.setText( " ");
pw2.setText( " ");
/*mail.setText( " ");
adress.setText( " ");*/
return;

}  
else   {
try   {

String   str   =   "insert   into   user   (name,pass,mail,adress)   Values( ' "
+   newname   +   " ', ' "   +   newpw   +   " ', ' "   +   newmail   +   " ', ' "
+   newadr   +   " ') ";

System.out.println(newname);
System.out.println(newpw);
System.out.println(newmail);
System.out.println(newadr);
stmt   =   con.createStatement();
int   flag=stmt.executeUpdate(str);
//System.out.println(flag);   //测试时候查看输出值  
if(flag==1){
JOptionPane.showMessageDialog(this,   "注册成功 ");
}
else{
JOptionPane.showMessageDialog(this, "注册失败 ");
}

}   catch   (SQLException   e)   {
e.printStackTrace();
}

}
}   catch   (SQLException   e)   {
e.printStackTrace();
}

try   {
rs.close();
stmt.close();
con.close();
}   catch   (SQLException   e)   {
e.printStackTrace();
}

}

private   void   check()   {
newname   =   username.getText();
newpw   =   pw1.getPassword();
newpw1   =   pw2.getPassword();
newmail   =   mail.getText();
newadr   =   adress.getText();
if   (newname.length()   ==   0)   {
JOptionPane.showMessageDialog(this,   "用户名不能为空 ");

}   else   if   (newpw.length   ==   0)   {
JOptionPane.showMessageDialog(this,   "密码不能为空 ");

}   else   if   (newpw1.length   ==   0)   {