myeclipse中的java链接数据库的代码问题。
packagedata;importjava.sql.*;publicclassdata{publicstaticvoidmain(String[]args){//加载J...
package data;
import java.sql.*;
public class data{
public static void main(String[] args) {
//加载JDBC驱动
Class.forName("com.mysql.jdbc.Driver");
//连接到数据库
String dbUrl="jdbc:mysql://localhost:3306/mypetshop";
try{ Connection c=DriverManager.getConnection(dbUrl,"root","fly");
System.out.println("Connection Successful!");
}
catch(SQLException e){
e.printStackTrace();
System.out.print("数据库连接失败");
} catch (ClassNotFoundException e)
{ e.printStackTrace();
System.out.print("驱动加载失败");}
//声明一个Statement对象
Statement s=c.createStatement();
//从STUDENT数据库中查询到记录集
ResultSet r=s.executeQuery("select * from cat");
while(r.next())
{
System.out.println(r.getString("idcat")+r.getString("cname")
+r.getString("cprice")+r.getString("ccolor"));
}
//关闭s和c
s.close();
c.close();
}
}
错误提示是:如图所示。即c这个变量有误。 展开
import java.sql.*;
public class data{
public static void main(String[] args) {
//加载JDBC驱动
Class.forName("com.mysql.jdbc.Driver");
//连接到数据库
String dbUrl="jdbc:mysql://localhost:3306/mypetshop";
try{ Connection c=DriverManager.getConnection(dbUrl,"root","fly");
System.out.println("Connection Successful!");
}
catch(SQLException e){
e.printStackTrace();
System.out.print("数据库连接失败");
} catch (ClassNotFoundException e)
{ e.printStackTrace();
System.out.print("驱动加载失败");}
//声明一个Statement对象
Statement s=c.createStatement();
//从STUDENT数据库中查询到记录集
ResultSet r=s.executeQuery("select * from cat");
while(r.next())
{
System.out.println(r.getString("idcat")+r.getString("cname")
+r.getString("cprice")+r.getString("ccolor"));
}
//关闭s和c
s.close();
c.close();
}
}
错误提示是:如图所示。即c这个变量有误。 展开
3个回答
展开全部
package data;
import java.sql.*;
public class Data{ //类名首字母大写
public static void main(String[] args) {
//加载JDBC驱动
Class.forName("com.mysql.jdbc.Driver");
//连接到数据库
String dbUrl="jdbc:mysql://localhost:3306/mypetshop";
try{ Connection c=DriverManager.getConnection(dbUrl,"root","fly");
if(c != null) { //这里要判断c这个对象是不是为空值,不加这一句话,下面的不管连接没有连接到数据库,都会执行
System.out.println("Connection Successful!");
}
}
catch(SQLException e){
e.printStackTrace();
System.out.print("数据库连接失败");
} catch (ClassNotFoundException e)
{ e.printStackTrace();
System.out.print("驱动加载失败");}
//声明一个Statement对象
Statement s=c.createStatement();
//从STUDENT数据库中查询到记录集
ResultSet r=s.executeQuery("select * from cat");
while(r.next())
{
System.out.println(r.getString("idcat")+r.getString("cname")
+r.getString("cprice")+r.getString("ccolor"));
}
//关闭s和c
s.close();
c.close();
}
}
import java.sql.*;
public class Data{ //类名首字母大写
public static void main(String[] args) {
//加载JDBC驱动
Class.forName("com.mysql.jdbc.Driver");
//连接到数据库
String dbUrl="jdbc:mysql://localhost:3306/mypetshop";
try{ Connection c=DriverManager.getConnection(dbUrl,"root","fly");
if(c != null) { //这里要判断c这个对象是不是为空值,不加这一句话,下面的不管连接没有连接到数据库,都会执行
System.out.println("Connection Successful!");
}
}
catch(SQLException e){
e.printStackTrace();
System.out.print("数据库连接失败");
} catch (ClassNotFoundException e)
{ e.printStackTrace();
System.out.print("驱动加载失败");}
//声明一个Statement对象
Statement s=c.createStatement();
//从STUDENT数据库中查询到记录集
ResultSet r=s.executeQuery("select * from cat");
while(r.next())
{
System.out.println(r.getString("idcat")+r.getString("cname")
+r.getString("cprice")+r.getString("ccolor"));
}
//关闭s和c
s.close();
c.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package cn.com.csuinfosoft.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLUtil {
private static SQLUtil instance = null;
public synchronized static SQLUtil getInstance() {
if (instance == null) {
instance = new SQLUtil();
}
return instance;
}
private SQLUtil() {
}
static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracl", "admin", "admin");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public void closeConection(Connection conn, Statement st, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void closeStatement(Statement st, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLUtil {
private static SQLUtil instance = null;
public synchronized static SQLUtil getInstance() {
if (instance == null) {
instance = new SQLUtil();
}
return instance;
}
private SQLUtil() {
}
static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracl", "admin", "admin");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public void closeConection(Connection conn, Statement st, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void closeStatement(Statement st, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
高清error和exception的区别
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询