Java数据库编程
求助:新手自学学Java就是磨难多啊!如题:Java链接mysql数据库,将sql字符串改为“select*fromtablename”可以正常查找到;但是将sql换成其...
求助:新手自学学Java就是磨难多啊!如题:Java链接mysql数据库,将sql字符串改为“select * from tablename”可以正常查找到;但是将sql换成其他的,如修改,插入,删除就报错了。。。无语中。。我确定我的sql代码没有错的,亲手在mysql下敲过的代码。下面是我写的代码和报错信息:
package package1;
import java.sql.*;
import java.util.*;
public class UseMySQlL
{
private ResultSet rs=null;
private Statement pstmt=null;
private Connection conn=null;
public void getConnection()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/abcd","mr_m","0000");
if(conn!=null)
System.out.println("已加载驱动!");
}
catch (ClassNotFoundException e)
{
System.out.println("未找到驱动类");
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public void execSQL()
{
try
{
if(conn==null)
getConnection();
String sql="delete from mytable5"+"where name='maoshilei'";
pstmt=conn.createStatement();
if(pstmt!=null)
System.out.println("已创建操作语句");
rs=pstmt.executeQuery(sql);
if(rs!=null)
System.out.println("已执行sql语句!");
while(rs.next())
{
System.out.println("找到一行");
for(int i=0;i<=3;i++)
System.out.print(rs.getString(i+1)+"\t");
System.out.println("\n");
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
void close()
{
try
{
if(rs!=null)
rs.close();
if(pstmt!=null)
rs.close();
if(conn!=null)
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void main(String []args)
{
UseMySQlL use=new UseMySQlL();
use.getConnection();
use.execSQL();
use.close();
}
}
报错信息:
java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:413)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1297)
at package1.UseMySQlL.execSQL(UseMySQlL.java:44)
at package1.UseMySQlL.main(UseMySQlL.java:83)
Exception in thread "main" java.lang.NullPointerException
at package1.UseMySQlL.close(UseMySQlL.java:69)
at package1.UseMySQlL.main(UseMySQlL.java:84)
已加载驱动!
已创建操作语句 展开
package package1;
import java.sql.*;
import java.util.*;
public class UseMySQlL
{
private ResultSet rs=null;
private Statement pstmt=null;
private Connection conn=null;
public void getConnection()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/abcd","mr_m","0000");
if(conn!=null)
System.out.println("已加载驱动!");
}
catch (ClassNotFoundException e)
{
System.out.println("未找到驱动类");
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public void execSQL()
{
try
{
if(conn==null)
getConnection();
String sql="delete from mytable5"+"where name='maoshilei'";
pstmt=conn.createStatement();
if(pstmt!=null)
System.out.println("已创建操作语句");
rs=pstmt.executeQuery(sql);
if(rs!=null)
System.out.println("已执行sql语句!");
while(rs.next())
{
System.out.println("找到一行");
for(int i=0;i<=3;i++)
System.out.print(rs.getString(i+1)+"\t");
System.out.println("\n");
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
void close()
{
try
{
if(rs!=null)
rs.close();
if(pstmt!=null)
rs.close();
if(conn!=null)
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void main(String []args)
{
UseMySQlL use=new UseMySQlL();
use.getConnection();
use.execSQL();
use.close();
}
}
报错信息:
java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:413)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1297)
at package1.UseMySQlL.execSQL(UseMySQlL.java:44)
at package1.UseMySQlL.main(UseMySQlL.java:83)
Exception in thread "main" java.lang.NullPointerException
at package1.UseMySQlL.close(UseMySQlL.java:69)
at package1.UseMySQlL.main(UseMySQlL.java:84)
已加载驱动!
已创建操作语句 展开
展开全部
rs=pstmt.executeQuery(sql);
改成
boolean flag = pstmt.execute(sql);
executeQuery是查询用方法。不能执行delete
改成
boolean flag = pstmt.execute(sql);
executeQuery是查询用方法。不能执行delete
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
第34行 rs = pstmt.executeQuery(sql); 明明你的sql语句不是执行的查询工作 你还用executeQuery
改成execute(sql);
改成execute(sql);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
insert和update和delete都不需要返回结果集ResultSet,所以用户executeQuery ;
而select需要有返回的结果集,要不看什么数据呢, 所以用户executeUpdate
而select需要有返回的结果集,要不看什么数据呢, 所以用户executeUpdate
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询