Java连接MySQL数据库
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/epet",root,root);
//sql语句
String sql="________________________________________________";
stmt=conn.prepareStatement(sql);
stmt.setString(___,____);
System.out.println(sql);
rs=stmt.executeQuery();
if(rs.next()){//SQL语句输入成功
System.out.println("插入成功");
}else{
System.out.println("输入失败");
}
}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
if(rs != null){
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(stmt != null){
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}