how can i insert image in matisse database using SQL query? - java

i have to insert an image into the matisse database. how can i insert it through sql insert statement in java?

You can use a java.sql.PreparedStatement to insert a blob into your database.
Example code:
try{
Connection con = getYourDatabaseConnection();
PreparedStatement statement = con.prepareStatement("INSERT INTO YOUR_TABLE(YOUR_BLOB_COLUMN) VALUES (?)";
statement.setObject(1, yourImage);
statement.execute();
} catch(SQLException e){
e.printStackTrace();
}

Related

Java PreparedStatement Comment on Table

I'm familiar with using java prepared statements to insert/update on a table. In oracle you can add a comment on a table, how would I use a preparedstatement to do that?
This was my initial attempt with no luck;
PreparedStatement stmt = con.prepareStatement("comment on table my_table is q'[?]'");
stmt.setString(1, description);
stmt.executeUpdate();
You can use system Oracle table and set comment there with PreparedStatement, like this:
PreparedStatement stmt = con.prepareStatement(
"UPDATE user_tab_comments SET comments = ? WHERE table_name = 'my_table'");
Or try to use simple statement:
String commentOnTableSQL = String.format("COMMENT ON TABLE my_table is '%s'", comment);
Statement statement = dbConnection.createStatement();
statement.execute(commentOnTableSQL);

Inserting auto generated id in sqllite using java [duplicate]

This question already has an answer here:
How to perform an SQL insert in Java
(1 answer)
Closed 7 years ago.
I am writing java application using sqllite
public Product createProduct(String productName) throws SQLException {
String query = "INSERT into tbl_Product (name) values (?) ";
PreparedStatement preparedStatement = null;
Connection connection = null;
ResultSet rs = null;
Product product = null;
try {
connection = ConnectionFactory.getConnection();
preparedStatement = connection.prepareStatement(query);
//preStatement.setInt(1, 0);
preparedStatement.setString(1, productName);
preparedStatement.executeUpdate();
} finally {
//preparedStatement.close();
DbUtil.close(rs);
DbUtil.close(preparedStatement);
DbUtil.close(connection);
}
return product;
}
My product table have (ID,Name) column, where Id is auto generated. What all java changes are required so that preStatement can insert auto generated id in db.
String query = "INSERT into tbl_Product values (?) ";
ResultSet rs = null;
Product product = null;
try {
connection = ConnectionFactory.getConnection();
preStatement = (PreparedStatement) connection.prepareStatement(query);
preStatement.setString(1, productName);
rs = preStatement.executeQuery();
String sqlQuery = "INSERT into tbl_Product values (?)
PreparedStatement stmt = conn.prepareStatement(sqlQuery);
stmt.setString( 1, "val" ); //nameText variable contains the text of the jtextfield)
stmt.executeUpdate();
use execute update instead of execute query.
executeQuery():
Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.
executeUpdate():
Executes the SQL statement in this PreparedStatement object, which must be an SQL INSERT, UPDATE or DELETE statement; or an SQL statement that returns nothing, such as a DDL statement.

how to insert value in ms access using java

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con1=DriverManager.getConnection("jdbc:odbc:MyDatabase");
st1=con1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
System.out.println("Connect database in BallMoves1.java .......");
/*the below line giving error*/
rs1 = st1.executeQuery("insert into highscore" + " (score) " + "values('"+score+"')");
System.out.println("Score is inserted..");
System.out.println("Score......."+score);
}catch(Exception e){ e.printStackTrace();}
/*highscore is table and attributes of table are (sid,score).
the resulting error is:
Connect database in BallMoves1.java .......
java.sql.SQLException: No ResultSet was produced
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(JdbcOdbcStatement.java:258)
at BallMoves1.move(BallMoves1.java:378)
at BallMoves1.run(BallMoves1.java:223)
at java.lang.Thread.run(Thread.java:744)*/
You're calling executeQuery on something that isn't a query. But instead of calling execute with the same SQL, you should use a PreparedStatement:
String sql = "insert into highscore (score) values (?)";
try (Connection conn = DriverManager.getConnection("jdbc:odbc:MyDatabase");
PreparedStatement statement = conn.prepareStatement(sql)) {
statement.setInt(1, score);
statement.executeUpdate();
conn.commit();
}
Always use parameterized SQL, instead of plugging the values directly into the SQL - that protects you from SQL injection attacks, conversion errors, and hard-to-read code.
Use a try-with-resources statement (as I have) to automatically close the statement and connection at the end of the block.

MySQLSyntaxError inserting into MySQL database

I would like to insert data into the MySQL database that I am using.I get this ERROR.This is my code:
public boolean insertValues(String gisuniqkey,String objtype,String objkey,String lat,String lng)
{
int rc=-1;
try {
if(conn==null)
{
System.out.println("The connection was not initialized.");
return false;
}
Statement st=(Statement) conn.createStatement();
//lots of String concatenation,very expensive...BAD...use StringBuilder instead
String sql="Insert into ZMAPERP_GIS_DB (GISUNIQKEY,OBJTYPE,OBJKEY,LATITUDE,LONGITUDE) values("+gisuniqkey+","+objtype+","+objkey+","+lat+","+lng+");";
System.out.println(sql);
rc=st.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rc>0?true:false;
}
I think the insert statement should be like this, Use single quotes around the values:
"Insert into ZMAPERP_GIS_DB (GISUNIQKEY,OBJTYPE,OBJKEY,LATITUDE,LONGITUDE) values('"+gisuniqkey+"','"+objtype+"','"+objkey+"','"+lat+"','"+lng+"');";
Edit
Try it yourself in a command. What will work:
Option 1:
INSERT INTO ZMAPERP_GIS_DB (GISUNIQKEY,OBJTYPE,OBJKEY,LATITUDE,LONGITUDE)
VALUES(asdasd,asdasd,asdasd,asdasd);
Option 2:
INSERT INTO ZMAPERP_GIS_DB (GISUNIQKEY,OBJTYPE,OBJKEY,LATITUDE,LONGITUDE)
VALUES('asdasd','asdasd','asdasd','asdasd');
I would think that option 2 will work
Edit 2
But as mention in the comment. Using the prepare statement is recommended. Because this is open for sql injections
Reference:
SQL injection
JDBC PreparedStatement Example – Insert A Record
You should use PreparedStatements to avoid problems with building query strings.
PreparedStatement statement = null;
String insertSql = "Insert into ZMAPERP_GIS_DB (GISUNIQKEY,OBJTYPE,OBJKEY,LATITUDE,LONGITUDE) values (?,?,?,?,?)";
conn.setAutoCommit(false);
statement = conn.prepareStatement(insertSql);
statement.setString(1, gisuniqkey);
statement.setString(2, objtype);
statement.setString(3, objkey);
statement.setString(4, lat);
statement.setString(5, lng);
con.commit();
String sql="Insert into ZMAPERP_GIS_DB (GISUNIQKEY,OBJTYPE,OBJKEY,LATITUDE,LONGITUDE) values('"+gisuniqkey+"','"+objtype+"','"+objkey+"','"+lat+"','"+lng+"');";
Although, I would suggest you inject variable values using prepared statement.

Insertion in Excel failed

I tried inserting data into excel sheet using Java and ODBC, my URL is correct, the query to insert the data is executing, but the values are not inserted into excel sheet, i have made connection to commit and close. kindly help!
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:excelDsn;readonly=false;");
ps = con.prepareStatement("insert into [Sheet1$](FirstName, LastName) values (?,?)");
ps.setString(1, "AA");
ps.setString(2, "BB");
ps.execute();
con.commit();
con.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
for DDL statements like insert,delete and update use ps.executeUpdate();
As your case is to insert so replace ps.execute(); with ps.executeUpdate();

Categories

Resources