当前位置: 技术问答>java相关
关于数据库的问题
来源: 互联网 发布时间:2015-05-19
本文导语: 请问下列代码中 Statement st = null; ResultSet rs = null; Connection c = null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); c = DriverManager.getConnection("jdbc:odbc:JavaDatabase","sa",""); st = c.createStateme(Resu...
请问下列代码中
Statement st = null;
ResultSet rs = null;
Connection c = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection("jdbc:odbc:JavaDatabase","sa","");
st = c.createStateme(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet. CONCUR_UPDATABLE);
rs = st.executeQuery("select * from sys_user");
user_name = rs.getString("user_name");
user_password = rs.getString("user_password");
只要一到user_name = rs.getString("user_name");
user_password = rs.getString("user_password");
据出现错误,但是数据库连接没有问题.
谢谢!!
Statement st = null;
ResultSet rs = null;
Connection c = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection("jdbc:odbc:JavaDatabase","sa","");
st = c.createStateme(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet. CONCUR_UPDATABLE);
rs = st.executeQuery("select * from sys_user");
user_name = rs.getString("user_name");
user_password = rs.getString("user_password");
只要一到user_name = rs.getString("user_name");
user_password = rs.getString("user_password");
据出现错误,但是数据库连接没有问题.
谢谢!!
|
这样应该成功.
try{
String sql="select * from test";
statement=connection.prepareStatement(sql);
ResultSet result=statement.executeQuery();
while(result.next())
{
int nid=result.getInt("id");
String strid=new String("id "+nid);
System.out.println(strid);
String name=result.getString("username");
System.out.println("name "+name);
String sex=result.getString("old");
System.out.println("sex "+sex);
// int age=result.getInt("age");
// String strAge=new String(""+age);
// System.out.println("age "+strAge);
String phone=result.getString("birthday");
System.out.println("phone "+phone);
}
result.close();
statement.close();
}catch(Exception e)
{
System.out.println(e.toString());
}
try{
String sql="select * from test";
statement=connection.prepareStatement(sql);
ResultSet result=statement.executeQuery();
while(result.next())
{
int nid=result.getInt("id");
String strid=new String("id "+nid);
System.out.println(strid);
String name=result.getString("username");
System.out.println("name "+name);
String sex=result.getString("old");
System.out.println("sex "+sex);
// int age=result.getInt("age");
// String strAge=new String(""+age);
// System.out.println("age "+strAge);
String phone=result.getString("birthday");
System.out.println("phone "+phone);
}
result.close();
statement.close();
}catch(Exception e)
{
System.out.println(e.toString());
}
|
executeQuery返回的resultset,游标是指在第一行纪录之前的,你在读数据前,要用rs.next()向下移动一下。具体参看jdbc文档
|
中文字符的非常正确
追加的问题参照:
http://www.csdn.net/expert/topic/647/647009.xml?temp=.2385218
追加的问题参照:
http://www.csdn.net/expert/topic/647/647009.xml?temp=.2385218