Como inserir um valor no banco de dados ( mysql ) pelo java?
ME AJUDEM, POR FAVOR!!!
Olha como eu tentei:
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class DisplayAuthors
{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL = "jdbc:mysql://localhost/books";
public static void main( String args[] )
{
Connection connection = null;
Statement statement = null;
Statement statement2 = null;
try
{
Class.forName( JDBC_DRIVER );
connection = DriverManager.getConnection( DATABASE_URL, "jhtp6", "jhtp6" );
// AQUI ESTÁ O QUE NÃO FUNCIONA!!!!!
//statement2 = connection.createStatement();
//ResultSet resultSet2 = statement.executeQuery( "INSERT INTO authors(authorID, firstName, lastName) VALUES (5, 'Joelson', 'Lucena')" );
//statement2.close();
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery( "SELECT authorID, firstName, lastName FROM authors" );
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
System.out.println();
for( int i = 1; i <= numberOfColumns; i++ )
System.out.printf( "%-8s\t", metaData.getColumnName( i ) );
System.out.println();
while( resultSet.next() )
{
for( int i = 1; i <= numberOfColumns; i++ )
System.out.printf( "%-8s\t", resultSet.getObject( i ) );
System.out.println();
}
}
catch( SQLException sqlException )
{
sqlException.printStackTrace();
System.exit( 1 );
}
catch( ClassNotFoundException classNotFound )
{
classNotFound.printStackTrace();
System.exit( 1 );
}
finally
{
try
{
statement.close();
//statement2.close();
connection.close();
}
catch( Exception exception )
{
exception.printStackTrace();
System.exit( 1 );
}
}
}
}