ITPub博客

首页 > 数据库 > MySQL > java与mysql相连一例

java与mysql相连一例

原创 MySQL 作者:rainytag 时间:2007-06-24 09:30:00 0 删除 编辑
在正确安装mysql jdbc 有前提下:





import java.sql.*;
import java.lang.*;
public class ch1
{
public static void main(String args[ ])
{
String driver ="org.gjt.mm.mysql.Driver";
String url ="jdbc:mysql://127.0.0.1:3306/jsptest";
String user ="hukn";
String password="hukn";
try
{
Class.forName(driver);
}
catch(Exception E)
{
System.out.print("无法加载驱动:"+driver);
}
try
{
Connection con=DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("打开数据库成功");
Statement smt=con.createStatement();
smt.executeUpdate("insert into test(name,adress) values('中国','成都')");
smt.close();
con.close();
}
catch(SQLException SE)
{
System.out.print("打开数据库失败");
}
}
}
[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9650775/viewspace-920826/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-01

  • 博文量
    652
  • 访问量
    2207545