62,620
社区成员
发帖
与我相关
我的任务
分享


public class JdbcUtils
{
private static String url = "jdbc:mysql://localhost:3306/jdbc";
private static String user = "root";
private static String password = "0825";
static
{
//注册驱动
try
{
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException
{
return DriverManager.getConnection(url, user, password);
}
public static void free(ResultSet rs, Statement st, Connection conn)
{
try
{ if(rs != null)
rs.close();
} catch (SQLException e)
{
e.printStackTrace();
}finally
{
try
{ if(rs != null)
st.close();
} catch (SQLException e)
{
e.printStackTrace();
}finally
{
try
{ if(rs != null)
conn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}
public static void create() throws SQLException
{
Connection conn = null;
Statement st = null;
ResultSet rs = null;
//建立连接
conn = JdbcUtils.getConnection();
//创建语句
st = conn.createStatement();
String sql = "insert into user(name, birthday, money)value('name1','1997-01-01',500)";
//执行语句
int i = st.executeUpdate(sql);
System.out.println("i= :" + i );
//释放资源
JdbcUtils.free(rs, st, conn);
}