Ejemplo Insertar Registros
Image 
Para insertar registros debe primero armarse la conexión con la base de datos y luego realizar la inserción. Un ejemplo de este tipo es:
```
public static void main(String[] args)
{
try
{
// create a mysql database connection
String myDriver = "org.gjt.mm.mysql.Driver";
String myUrl = "jdbc:mysql://localhost/test";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, "root", "");
Statement st = conn.createStatement();
// note that i'm leaving "date_created" out of this insert statement
st.executeUpdate("INSERT INTO users (first_name, last_name, is_admin, num_points) "+"VALUES ('Fred', 'Flinstone', false, 10000)");
conn.close();
}
catch (Exception e)
{
System.err.println("Got an exception!");
System.err.println(e.getMessage());
}
}
```
ID:(8875, 0)
Ejemplo Listar Registros
Note 
Para listar registros debe primero armarse la conexión con la base de datos, luego realizar la busqueda y finalmente listar el resultado. Un ejemplo de este tipo es:
```
public static void main(String[] args)
{
try
{
// create our mysql database connection
String myDriver = "org.gjt.mm.mysql.Driver";
String myUrl = "jdbc:mysql://localhost/fsca107";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(fsca107, "root", "");
// our SQL SELECT query.
// if you only need a few columns, specify them by name instead of using "*"
String query = "SELECT * FROM users";
// create the java statement
Statement st = conn.createStatement();
// execute the query, and get a java resultset
ResultSet rs = st.executeQuery(query);
// iterate through the java resultset
while (rs.next())
{
int id = rs.getInt("id");
String firstName = rs.getString("first_name");
String lastName = rs.getString("last_name");
Date dateCreated = rs.getDate("date_created");
boolean isAdmin = rs.getBoolean("is_admin");
int numPoints = rs.getInt("num_points");
// print the results
System.out.format("%s, %s, %s, %s, %s, %s
", id, firstName, lastName, dateCreated, isAdmin, numPoints);
}
st.close();
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
```
ID:(8874, 0)
