当前位置: 技术问答>java相关
数据库的问题。。。。。。。。。
来源: 互联网 发布时间:2015-01-03
本文导语: 由于在使用 Statement stmt=conn.createStatement(); 时不能对数据库进行更新操作。所以我在连接时用了以下的语句。 Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ...
由于在使用
Statement stmt=conn.createStatement();
时不能对数据库进行更新操作。所以我在连接时用了以下的语句。
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
但是一用RS.next()之类的语句是老是返回一个NULL,为什么我该怎么解决向数据库更新的问题?
急。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
Statement stmt=conn.createStatement();
时不能对数据库进行更新操作。所以我在连接时用了以下的语句。
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
但是一用RS.next()之类的语句是老是返回一个NULL,为什么我该怎么解决向数据库更新的问题?
急。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
|
我写了两个bean,你试试吧
package mydb;
import java.sql.*;
public class conn
{
String DBDriver = "org.gjt.mm.mysql.Driver";
String ConnStr = "jdbc:mysql://localhost:3306/net";
Connection conn = null;
public conn() {
try
{
Class.forName(DBDriver);
}
catch (java.lang.ClassNotFoundException error)
{
System.err.println(error.getMessage());
}
}
public Connection getconn() {
try
{
conn = DriverManager.getConnection(ConnStr,"","");
return conn;
}
catch (SQLException errorc)
{
System.err.println(errorc.getMessage());
return null;
}
}
public void close() {
try {
conn.close();
}
catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}
package mydb;
import java.sql.*;
public class result
{
Connection m_conn = null;
Statement m_stmt = null;
ResultSet m_rs = null;
public result() {
}
public void setconn(Connection conn) {
m_conn=conn;
try {
m_stmt = m_conn.createStatement();
}
catch (SQLException ex) {
System.err.println("ExecuteQuery: " + ex.getMessage());
}
}
public ResultSet executeQuery(String sql) {
try {
m_rs = m_stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.err.println("ExecuteQuery: " + ex.getMessage());
}
return m_rs;
}
public void executeUpdate(String sql) {
try {
m_stmt.executeUpdate(sql);
}
catch (SQLException ex) {
System.err.println("ExecuteQuery: " + ex.getMessage());
}
}
public void close() {
try {
m_rs.close();
m_stmt.close();
m_conn.close();
}
catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}
package mydb;
import java.sql.*;
public class conn
{
String DBDriver = "org.gjt.mm.mysql.Driver";
String ConnStr = "jdbc:mysql://localhost:3306/net";
Connection conn = null;
public conn() {
try
{
Class.forName(DBDriver);
}
catch (java.lang.ClassNotFoundException error)
{
System.err.println(error.getMessage());
}
}
public Connection getconn() {
try
{
conn = DriverManager.getConnection(ConnStr,"","");
return conn;
}
catch (SQLException errorc)
{
System.err.println(errorc.getMessage());
return null;
}
}
public void close() {
try {
conn.close();
}
catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}
package mydb;
import java.sql.*;
public class result
{
Connection m_conn = null;
Statement m_stmt = null;
ResultSet m_rs = null;
public result() {
}
public void setconn(Connection conn) {
m_conn=conn;
try {
m_stmt = m_conn.createStatement();
}
catch (SQLException ex) {
System.err.println("ExecuteQuery: " + ex.getMessage());
}
}
public ResultSet executeQuery(String sql) {
try {
m_rs = m_stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.err.println("ExecuteQuery: " + ex.getMessage());
}
return m_rs;
}
public void executeUpdate(String sql) {
try {
m_stmt.executeUpdate(sql);
}
catch (SQLException ex) {
System.err.println("ExecuteQuery: " + ex.getMessage());
}
}
public void close() {
try {
m_rs.close();
m_stmt.close();
m_conn.close();
}
catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}
|
执行"select sysdate from dual";
rs.next()回怎样?
rs.next()回怎样?
|
有呀,比如
Connection conn=null;
Preparedstatement pstmt;
String sql="insert into test(id,name) values(1,'AAA')";
conn=DriverManager.getConnectiom(......);
pstmt=conn.prepareStatement(sql);
pstmt.executeUpdate();
Connection conn=null;
Preparedstatement pstmt;
String sql="insert into test(id,name) values(1,'AAA')";
conn=DriverManager.getConnectiom(......);
pstmt=conn.prepareStatement(sql);
pstmt.executeUpdate();
|
我这样用没有你说的返回NULL的问题呀
|
你检查一下你的Connection建好了吗?