Saltar al contenido

resultados de salida del ejemplo de código SQL select java

Nuestro team redactor ha estado mucho tiempo investigando respuestas a tus interrogantes, te dejamos la solución y deseamos que te resulte de mucha ayuda.

Ejemplo 1: cómo escribir consultas en java

importjava.sql.*;/**
 * A Java MySQL SELECT statement example.
 * Demonstrates the use of a SQL SELECT statement against a
 * MySQL database, called from a Java program.
 * 
 * Created by Alvin Alexander, http://alvinalexander.com
 */publicclassJavaMysqlSelectExamplepublicstaticvoidmain(String[] args)try// create our mysql database connectionString myDriver ="org.gjt.mm.mysql.Driver";String myUrl ="jdbc:mysql://localhost/test";Class.forName(myDriver);Connection conn =DriverManager.getConnection(myUrl,"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 statementStatement st = conn.createStatement();// execute the query, and get a java resultsetResultSet rs = st.executeQuery(query);// iterate through the java resultsetwhile(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 resultsSystem.out.format("%s, %s, %s, %s, %s, %sn", id, firstName, lastName, dateCreated, isAdmin, numPoints);
      st.close();catch(Exception e)System.err.println("Got an exception! ");System.err.println(e.getMessage());

Ejemplo 2: crear declaración en jdbc

Connection=Helps our java project connect todatabaseStatement=Helpstowrite and execute SQL query
ResultSet=ADataStructure where the data from query result stored
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++After succesfully created the connect
next step is STATEMENT

Statement statement = connection.createStatement();We use createStatement() method tocreate
the statement from our connection.-The result we get from this type of statement
can only move from top tobottom,
not other way around

Statement statement = connection.createStatement(ResultSet TYPE_SCROLL_INSENSITIVE
,ResultSet CONCUR_READ_ONLY);-The result we get from this type of
statement can freely move between rows

Once we have statement we can run
the query and get the result toResultSet format 
		importjava.sql.ResultSet;We use the method executeQuery()toexecute our queries

ResultSet result = statement.executeQuery("Select * from employees");

Si te mola el asunto, tienes la opción de dejar una crónica acerca de qué le añadirías a esta reseña.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *