81,092
社区成员
发帖
与我相关
我的任务
分享
import java.sql.*;
public class test3{
//添加了声明异常 throws Exception 因为你的run()方法可能会抛异常,这里throws掉
public static void main(String args[]) throws Exception{
System.out.println("开始...");
Yewu yw=new Yewu();
yw.run();
}
}
//主体业务部份
class Yewu{
//开始执行
void echo(String str){
System.out.println(str);
}
//此处同理,添加了throws Exception
void run() throws Exception{
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
//此处我修改了地址,因为我不知道你的Tomcat地址是否正确,默认的是://127.0.0.1或者localhost
conn = DriverManager.getConnection("jdbc:mysql://192.168.0.77:3306/test","root","sz123");
} catch (SQLException ex) {
// handle any errors
this.echo("SQLException: " + ex.getMessage());
this.echo("SQLState: " + ex.getSQLState());
this.echo("VendorError: " + ex.getErrorCode());
}
this.echo("连接成功");
}
}