求助 为什么rs = stmt.executeQuery(strsql); 这一步执行不了啊
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;impor...
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import java.sql.*;
public class test
{
public static Connection con=null;
public static Statement stmt=null;
public static ResultSet rs=null;
public static String strsql=null;
//数据库test的URL
public static String url="jdbc:mysql://localhost/testdb?user=root&password=123456&useUnicode=true&characterEncoding=8859_1";
//数据驱动途径
public static String className="com.mysql.jdbc.Driver";
public static String userName="root";//用户名
public static String password="123456";//密码
public static void ConnectionDB()
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("load JDBC Driver successful!");
} catch (Exception e)
{
System.out.println("load Driver false!");
}
}
public static void getConnection()
{
try
{
con = DriverManager.getConnection(url,userName,password);
System.out.println("Connection database successful!");
} catch (SQLException e)
{
System.out.println("Connection database false");
}
}
public static void ExecuteSQL()
{
try
{
strsql = "select * from testdb";
stmt = con.createStatement();
rs = stmt.executeQuery(strsql);
//if(strsql != null)
System.out.println("correct");
//else System.out.println("false");
System.out.println(rs.getString("name"));
//boolean more=rs.next();
System.out.println(rs.next());
while (rs.next())
{
//System.out.println("hello");
System.out.println(rs.getString("name"));
}
System.out.println("ExecuteSQL successful!");
} catch (SQLException e)
{
}
}
public static void close()
{
try
{
con.close();
stmt.close();
//rs.close();
} catch (SQLException e)
{
}
}
public static void main(String[] args) {
ConnectionDB();
getConnection();
ExecuteSQL();
close();
}
} 展开
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import java.sql.*;
public class test
{
public static Connection con=null;
public static Statement stmt=null;
public static ResultSet rs=null;
public static String strsql=null;
//数据库test的URL
public static String url="jdbc:mysql://localhost/testdb?user=root&password=123456&useUnicode=true&characterEncoding=8859_1";
//数据驱动途径
public static String className="com.mysql.jdbc.Driver";
public static String userName="root";//用户名
public static String password="123456";//密码
public static void ConnectionDB()
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("load JDBC Driver successful!");
} catch (Exception e)
{
System.out.println("load Driver false!");
}
}
public static void getConnection()
{
try
{
con = DriverManager.getConnection(url,userName,password);
System.out.println("Connection database successful!");
} catch (SQLException e)
{
System.out.println("Connection database false");
}
}
public static void ExecuteSQL()
{
try
{
strsql = "select * from testdb";
stmt = con.createStatement();
rs = stmt.executeQuery(strsql);
//if(strsql != null)
System.out.println("correct");
//else System.out.println("false");
System.out.println(rs.getString("name"));
//boolean more=rs.next();
System.out.println(rs.next());
while (rs.next())
{
//System.out.println("hello");
System.out.println(rs.getString("name"));
}
System.out.println("ExecuteSQL successful!");
} catch (SQLException e)
{
}
}
public static void close()
{
try
{
con.close();
stmt.close();
//rs.close();
} catch (SQLException e)
{
}
}
public static void main(String[] args) {
ConnectionDB();
getConnection();
ExecuteSQL();
close();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询