菜鸟问题~关于数据库的链接 真的很菜的 快来抢分吧!

ORACLE800 2008-04-26 12:03:16
刚刚接触数据库连接~下面的代码我想问两个问题:
一个是像我这种string url的写法 为什么链接不上数据库?(帐号密码都是对的);
第二个问题是,如果我Statement st 放在注释那里(看看代码^_^)也是错的 why?
劳驾大家啦 问题解决马上给分 因为不想丢人太久 呵呵
package jdbctest1;
import java.sql.*;

public class test1
{
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver");

}
catch(ClassNotFoundException e)
{
System.out.println("无法连接数据库"+e.getMessage());
}


String url="jdbc:mysql://localhost:3306/mysql?user=root;password=123456";
try
{
Connection conn=DriverManager.getConnection(url);
Statement st=conn.createStatement();
}
catch(Exception e)
{
e.printStackTrace();
}

//Statement st=conn.createStatement();如果放在这里就会出错 为什么?



}

}
...全文
109 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
huanggeng_8552 2008-04-26
  • 打赏
  • 举报
回复

try(){
Statement st=conn.createStatement();//这样就不会有错了

}catch(){}
我是风 2008-04-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bootupnow 的回复:]
半夜蹭分!
1. 貌似要:String url="jdbc:mysql://localhost:3306/mysql?user=root&password=123456";
2. conn在作用域不对,你是在try里声明的!
[/Quote]
俺也蹭点。。。
lstep 2008-04-26
  • 打赏
  • 举报
回复
第二个问题, 因为 conn.createStatement(); 会抛SQLException. 它要么在try/catch块中,要么在main()的定义里,声明throws SQLException。
JadoNet 2008-04-26
  • 打赏
  • 举报
回复
有错误报吗?
能不能贴出来
bootupnow 2008-04-26
  • 打赏
  • 举报
回复
半夜蹭分!
1. 貌似要:String url="jdbc:mysql://localhost:3306/mysql?user=root&password=123456";
2. conn在作用域不对,你是在try里声明的!

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧