¿Cómo cambiar el nombre de la base de datos y la tabla usando Java?

i try to understand this part of code:

Properties details= new Properties();
details.load(new FileInputStream("details.properties"));
String userName = details.getProperty("root");
String password = details.getProperty("mysqlpassword");

String url = "jdbc:mysql://localhost/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
PreparedStatement st = conn.prepareStatement("insert into 'Email_list' values(?)");
for(String mail:mails)

i understand that test database is a default database. but if i want to use an existing database, i will just modify test to another database name isn't it? If yes how do i modify my code if my new database is Test2 with table name Email which contains mail column with varchar(100)

i try to replace test by Test2 Email_list by Email but i don't know where to put the column name mail. Thank you for help

preguntado el 01 de febrero de 12 a las 22:02

I believe you are not getting Database connection established output... right?? this is because you are missing PORT number in your connection.... String url = "jdbc:mysql://localhost/test"; debiera ser String url = "jdbc:mysql://localhost:3306/test";.. Here 3306 is PORT number... -

2 Respuestas

El INSERT statement you use omits the columns.

INSERT INTO tablename VALUES (1, 2, 3)

can be written if the table has three columns and for all three columns values are provided.

If some columns can be left empty or have default values, you can write

INSERT INTO tablename (column1, column2) VALUES (1, 2)

In this cas the value for column3 is null or the default value.

So in your case the column name is put nowhere.

Respondido 02 Feb 12, 02:02

Can you please attach a tutorial link for the OP. - RanRag

OK so i try this PreparedStatement st= conn.prepareStatement("insert into 'mail' values(Email_list) values"); - Pacific

This will probably not work because values() contiene el valores, not the column names. Try to use the mysql client to work with your database directly. Come back to Java later. - user647772

OK so i try this PreparedStatement st= conn.prepareStatement("insert into Email (mail) values (?)"; i have an error Cannot connect to the database and com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database 'Test2' - Pacific

Estás perdido PORT number in your connection string...

String url = "jdbc:mysql://localhost/test"; debiera ser String url = "jdbc:mysql://localhost:PORT_NUMBER/test"; como String url = "jdbc:mysql://localhost:3306/test";

Let me know if you have any queries...

Also, Check below how Prepared Statement funciona

import java.sql.*;

public class TwicePreparedStatement{
  public static void main(String[] args) {
  System.out.println("Twice use prepared statement example!\n");
  Connection con = null;
  PreparedStatement prest;
  con = DriverManager.getConnection("jdbc:mysql:
  String sql = "SELECT * FROM movies WHERE year_made = ?";
  prest = con.prepareStatement(sql);
  ResultSet rs1 = prest.executeQuery();
  System.out.println("List of movies that made in year 2002");
  while (rs1.next()){
  String mov_name = rs1.getString(1);
  int mad_year = rs1.getInt(2);
  System.out.println(mov_name + "\t- " + mad_year);
  ResultSet rs2 = prest.executeQuery();
  System.out.println("List of movies that made in year 2003");
  while (rs2.next()){
  String mov_name = rs2.getString(1);
  int mad_year = rs2.getInt(2);
  System.out.println(mov_name + "\t- " + mad_year);
  catch (SQLException s){
  System.out.println("SQL statement is not executed!");
  catch (Exception e){

¡Buena suerte!

Respondido 02 Feb 12, 07:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.