一、使用MySQL建表:
二、编写java程序:
1、添加MySQL的jdbc类库
2、加载MySQL的jdbc驱动
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver");//加载mysql数据库驱动程序
System.out.println("Success loading Mysql Drives");
}
catch (Exception e)
{
System.out.println("Errot loading Mysql Driver!");
e.printStackTrace();
}
}
3、建立连接:
DriverManager.getConnection(url, user, password);
Connection ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","7424994");
4、创建sql对象:
Statement statement = ct.createStatement();
执行插入sql语句:
statement.executeUpdate("insert into student values" +"('100003','zzx','249974',21,'810620174@qq.com')");
完整代码:
package com.zzx.jdbcTest;
import java.sql.*;
public class TestJdbc
{
public static void main(String[] args)
{
Connection ct = null;
Statement statement = null;
try
{
Class.forName("com.mysql.jdbc.Driver");//加载mysql数据库驱动程序
System.out.println("Success loading Mysql Drives");
ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","7424994");
statement = ct.createStatement();
statement.executeUpdate("insert into student values" +
"('100003','zzx','249974',21,'810620174@qq.com')");
}
catch (Exception e)
{
System.out.println("Errot loading Mysql Driver!");
e.printStackTrace();
}
finally
{
if(statement !=null)
{
try {
statement.close();
}
catch (Exception e2)
{
e2.printStackTrace();
}
statement = null;
}
if(ct !=null)
{
try {
ct.close();
}
catch (Exception e2)
{
e2.printStackTrace();
}
ct = null;
}
}
}
}