Basically I have Column row with the value of:
ID Title
----- ----------------------------------
| 1 |ماهر زين - عليك صلى الله (Official) |
----- ----------------------------------
-
String Title = null;
DBConnect Database = new DBConnect();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
con = Database.getcon();
String query2="SELECT TITLE FROM table WHERE ID=?";
ps = con.prepareStatement(query2);
ps.setInt(1, 1);
rs=ps.executeQuery();
if(rs.next()){
Title=rs.getString(1);
System.out.println(Title);
}
} finally {
if(ps != null)
ps.close();
if(rs != null)
rs.close();
if(con != null)
con.close();
}
To connect to the DB I Do:
public DBConnect(){
try{
Class.forName("com.mysql.jdbc.Driver");
String unicode="useSSL=false&useUnicode=yes&characterEncoding=UTF-8";
con = DriverManager.getConnection("jdbc:mysql://localhost:15501/db?"+unicode, "root", "pwd");
st = con.createStatement();
}catch(Exception ex){
System.out.println(ex.getMessage());
System.out.println("couldn't connect!");
}
}
But the Output I get is: ????? ??? ????? ? ??? ?? ????? (Official)
I also have tried:
ALTER DATABASE <name> CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE <table_name> CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
-
I also tried:
select
c.character_set_name
from information_schema.tables as t,
information_schema.collation_character_set_applicability as c
where c.collation_name = t.table_collation
and t.table_schema = "duckdb"
and t.table_name = "stackscontents";
The output I get is `utf8mb4
FYI: My my.ini file says default-character-set=utf8
Yet still i'm getting the output as: ????? ??? ????? ? ??? ?? ????? (Official)
All replies are much appreciated
Related
The problem is that I am trying to set a wild card in a PreparedStatement but the setString statement is giving me the error above.
I have tried changing it to a setObeject statement with multiple different types like Types.VARCHAR. I have tried declaring the PreparedStatement in different places, and I have tried declaring 'name' in the method and in the class.
public String getTemplateText(String name) {
try (
Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT templateText FROM TEMPLATE WHERE " +
"templateTag = ?");
stmt.setString(1 , name); // this is the line that has the problem!
ResultSet rs = stmt.executeQuery()
) {
System.out.println("Set Text...");
String tempText = rs.getString("templateText");
return tempText;
} catch (SQLException e) {
e.printStackTrace();
}
return "";
}
/* this is the SQL code for the table that I am trying to query */
CREATE TABLE TEMPLATE
(
templateID INTEGER PRIMARY KEY IDENTITY(1,1)
, templateText TEXT
, templateTag CHAR(25)
);
You can't set the stmt parameter in your try-with-resources (because binding the parameter is void and not closable). Instead, you can nest a second try-with-resources after you bind the parameter. Like,
public String getTemplateText(String name) {
try (Connection conn = getConnection();
PreparedStatement stmt = conn
.prepareStatement("SELECT templateText FROM TEMPLATE WHERE " +
"templateTag = ?")) {
stmt.setString(1, name);
try (ResultSet rs = stmt.executeQuery()) {
System.out.println("Set Text...");
String tempText = rs.getString("templateText");
return tempText;
}
} catch (SQLException e) {
e.printStackTrace();
}
return "";
}
Currently this is my code to update my sql, and I'm getting a syntax error. the carid is being passed in from the front end. Can anyone assist me with fixing the syntax.
String carid = req.getParameter("id");
int rs;
Connection conn = null;
java.sql.PreparedStatement st= null;
String nativeSQL = "";
try {
Context ctx = new InitialContext();
Context env = ( Context )ctx.lookup( "java:comp/env" );
DataSource ds = ( DataSource )env.lookup( "jdbc/carRentalSystem");
conn = ds.getConnection();
st = conn.prepareStatement("update cardetails SET Availability = Unavailable where id='"+ carid+ "'");
st.clearParameters();
rs= st.executeUpdate();
if(rs != 0) {
res.sendRedirect("carRental.jsp");
return;
}else {
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try{ if(st != null ) st.close(); } catch(java.sql.SQLException e){}
try{ if(conn != null ) conn.close(); } catch(java.sql.SQLException e){}
}
}
Change the sql query line to
st = conn.prepareStatement("update cardetails SET Availability = 'Unavailable' where id='"+ carid+ "'");
Strings should be wrapped with quotes :
"update cardetails SET Availability = 'Unavailable' where id='"+ carid+ "'"
I am trying to connect my java application to hostinger.in
I tried this code but no output is displayed.Is this code correct
enter code here
Class.forName(\"com.mysql.jdbc.Driver\");
conn = DriverManager.getConnection(\"jdbc:mysql://mysql.hostinger.in/u869878874_stock\" ,\"u869878874_stock\", \"bhavin\");
stmt = conn.prepareStatement(\"SELECT * FROM logintbl\");
ResultSet rs=null;
rs=stmt.executeQuery();
while (rs.next())
{
usr =usr+rs.getString(\"userid\");
}
jLabel1.setText(usr);
I tried this code but no output is displayed
Is this code correct
Stop using escape sequence in your code and start using string directly.
https://docs.oracle.com/javase/tutorial/java/data/characters.html
Ex.
Class.forName("com.mysql.jdbc.Driver")
Changes to your code.
Connection connection = null;
PreparedStatement pStatement = null;
ResultSet resultSet = null;
String SELECT_SQL = "SELECT * FROM logintbl";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://<host>:<port>/<database_name>", "<user id>", "<password>");
pStatement = connection.prepareStatement(SELECT_SQL);
resultSet = pStatement.executeQuery();
for(;resultSet.next();){
System.out.println("User Id " + resultSet.getString("userid"));
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
I have searched to insert a value from application java into a mysql table.
Following there's my code. The result of it is a long row of error, but the first row is: java.sql.SQLException: Before start of result set
Connection con = null; //oggetto di connessione
try {
Class.forName("com.mysql.jdbc.Driver"); //connessione
con = DriverManager.getConnection("jdbc:mysql://localhost/autonoleggio", "root", "");//connessione vera e propria
//quarto va cambiato col nome del database creato
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM dati"); // persone ->tabella presente nel database
if(targa.getText().equals(rs.getString("Targa")))
{
Statement insideStatement = con.createStatement();
ResultSet insideResultsSet = insideStatement.executeQuery(
"INSERT INTO dati (Cliente) VALUES ('"+inserisci.getText()+"');"
);
/*String query;
query="INSERT INTO dati (Cliente) VALUES ('"+inserisci.getText()+"');";
PreparedStatement statement = con.prepareStatement(query);
statement.executeUpdate();
statement.close(); */
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(con != null) {
con.close();
}
}
The ResultSet object is sort of an iterator on the result returned from the database. Like any iterator, it starts before the first element, and needs to be advanced to point to it. In short, you're missing a call to ResultSet#next():
ResultSet rs = st.executeQuery("SELECT * FROM dati");
if (rs.next() && targa.getText().equals(rs.getString("Targa"))) {
// Here -^
I try to read out a table and write it into a csv file.
while (rs2.next())
{
Enumeration<String> en = sqlfields.keys();
while (en.hasMoreElements())
{
String field = en.nextElement();
String value = rs2.getString(field);
bw.write(Kapseln + value + Kapseln + Trennzeichen);
}
bw.newLine();
cur++;
}
But if the field is type: DateTime, I get this error message:
java.sql.SQLException: Cannot convert value
'0000-00-00 00:00:00' from column 12 to TIMESTAMP.
Why Java tries to convert to a Timestamp?
I like to get the value as String => rs2.getString(field)
set
zeroDateTimeBehavior="convertToNull"
in your JDBC connection properties. ref: http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-configuration-properties.html
EDIT that would make your connection string look like this:
jdbc:mysql://yourserver:3306/yourdatabase?zeroDateTimeBehavior=convertToNull
The following code is working fine. Check your code with this...
The fourth field of the emp table is jDate which is a DateTime data type.
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","test");
System.out.println("Connection is : " + con);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from emp");
//stmt = con.createStatement();
while(rs.next()) {
String value = rs.getString(4);
System.out.println(value);
}
System.out.println("Connection is : " + con);
con.close();