mysql与java的连接

来源:互联网 发布:农村淘宝开票信息 编辑:程序博客网 时间:2024/05/04 08:26
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("打开数据库失败");
}
}
}
 
原创粉丝点击