Contenido ordenado por fecha
|
Conexion libros
package conexionprueba;
import java.sql.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws SQLException {
conexion conex =new conexion();
Scanner lector=new Scanner(System.in);
int b=0;
while(b!=9){
conex.ObtenerConexion();
System.out.println("ingrese opcion\n" +
"1-listado de libros por codigo\n" +
"2-listado de libros por titulo\n" +
"3-listado libros con autores\n"+
"4-ingresar libros\n" +
"5-ingresar autor\n" +
"6-ingresar relacion libro autor\n"+
"7-borrar un libro\n"+
"8-actualizar un registro\n"+"" +
"9-salir\n");
System.out.print("ingrese opcion=>");
String x=lector.nextLine();
b=Integer.parseInt(x);
switch(b){
case 1:listado(conex); break;
case 2:listadoportitulo(conex);break;
case 3:listarconautores(conex);break;
case 4:ingresarlibro(conex);break;
case 5:ingresarautor(conex);break;
case 6:ingresarrelacionlibroautor(conex);break;
case 7:borrarlibro(conex);break;
case 8:actualizarregistro(conex);break;
case 9: b=9;break;
default:System.out.println("error: ingrese un valor correcto");
}
}
}
private static void listado(conexion conex) throws SQLException {
PreparedStatement st= conex.ObtenerConexion().prepareStatement("SELECT * FROM Libro ORDER BY numero_libros;");
ResultSet rs=st.executeQuery();
while(rs.next()){
System.out.print(rs.getString(1)+" - "+rs.getString(2)+"\n");
}
}
private static void listadoportitulo(conexion conex) throws SQLException{
PreparedStatement st= conex.ObtenerConexion().prepareStatement("SELECT Libro.[titulo] FROM Libro ORDER BY Libro.[titulo];");
ResultSet rs=st.executeQuery();
while(rs.next()){
for(int i=1;i<=rs.getMetaData().getColumnCount();i++){
System.out.print(rs.getString(i)+"\n");
}
} }
private static void listarconautores(conexion conex)throws SQLException {
PreparedStatement st= conex.ObtenerConexion().prepareStatement("SELECT Libro.numero_libros, Libro.titulo, Autor.nombre FROM Libro INNER JOIN (Autor INNER JOIN relacionLibroAutor ON Autor.[numero_autores] = relacionLibroAutor.[codAutor]) ON Libro.[numero_libros] = relacionLibroAutor.[codLibro];");
ResultSet rs=st.executeQuery();
while(rs.next()){
for(int i=1;i<=rs.getMetaData().getColumnCount();i++){
System.out.print(rs.getString(i)+"-");
}System.out.println();
}
} private static void ingresarlibro(conexion conex)throws SQLException {
PreparedStatement ps = conex.ObtenerConexion().prepareStatement("INSERT INTO Libro VALUES (?,?,?)");
Scanner linea = new Scanner(System.in);
int x1;
System.out.println("Ingrese Codigo del Libro=>");
x1=Integer.parseInt(linea.nextLine());
String x2;
System.out.println("Ingrese Nombre del Libro=>");
x2 = linea.nextLine();
int x3;
System.out.println("Ingrese Precio Libro=> ");
x3=Integer.parseInt(linea.nextLine());
ps.setInt(1,x1);
ps.setString(2,x2);
ps.setInt(3,x3);
ps.executeUpdate();
ps.close();}
private static void ingresarautor(conexion conex)throws SQLException{
PreparedStatement ps2 = conex.ObtenerConexion().prepareStatement("INSERT INTO Autor VALUES (?,?,?)");
Scanner linea = new Scanner(System.in);
int y1;
System.out.println("Ingrese Codigo del Autor=>");
y1 = Integer.parseInt(linea.nextLine());
String y2;
System.out.println("Ingrese Nombre del Autor=>");
y2 = linea.nextLine();
String y3;
System.out.println("Ingrese Nacionalidad del Autor=>");
y3 = linea.nextLine();
ps2.setInt(1, y1);
ps2.setString(2,y2);
ps2.setString(3, y3);
ps2.executeUpdate();
ps2.close();
}
private static void ingresarrelacionlibroautor(conexion conex) throws SQLException{
PreparedStatement ps3 = conex.ObtenerConexion().prepareStatement("INSERT INTO relacionLibroAutor VALUES (?,?)");
Scanner linea = new Scanner(System.in);
int x1;
System.out.println("Ingrese Codigo del Libro=>");
x1=Integer.parseInt(linea.nextLine());
int y1;
System.out.println("Ingrese Codigo del Autor=>");
y1 = Integer.parseInt(linea.nextLine());
ps3.setInt(1,x1);
ps3.setInt(2,y1);
ps3.executeUpdate();
ps3.close();
}
private static void borrarlibro(conexion conex) throws SQLException{
PreparedStatement st= conex.ObtenerConexion().prepareStatement("DELETE FROM libro WHERE titulo=?");
Scanner linea = new Scanner(System.in);
System.out.println("Ingrese nombre del libro");
String a1=linea.nextLine();
st.setString(1,a1);
st.executeUpdate();
st.close();
}
private static void actualizarregistro(conexion conex) throws SQLException{
PreparedStatement st= conex.ObtenerConexion().prepareStatement("UPDATE libro set precio=? where titulo=?");
Scanner linea = new Scanner(System.in);
System.out.println("Ingrese nombre del libro");
String a1=linea.nextLine();
System.out.println("Ingrese nuevo precio del libro");
int p1=linea.nextInt();
st.setInt(1,p1);
st.setString(2,a1);
st.executeUpdate();
st.close();
}
}
|
Reportar Contenido Inapropiado |
|
 |
Comparte en: |
Myspace |
StumbleUpon |
Mixx |
Live Spaces |
Fark |
|
 |
|
|
|
|
|