Solved: intellij java: package javax.servlet.http does not exist

While running java web project I got following error.

java: package javax.servlet does not exist

Two solve you need to add javax.servlet-3.0.jar file in your project or ide while executing code

you can download jar from this URL:

http://www.java2s.com/Code/Jar/j/Downloadjavaxservlet30jar.htm

Solution:

File -> Project Structure->Libraries

and add javax.servlet-3.0.jar library

Detail can found in this video

java: package javax.servlet.http does not exist

java mariadb executeQuery right syntax to use near ‘? mapping exception

I was using following code

String sql = "select * from user where username=? and password=?";
PreparedStatement ps =connection.prepareStatement(sql);
ps.setString(1, username);

ps.setString(2, password);
ResultSet rs=ps.executeQuery(sql);
while (rs.next()) {
user= new User();
user.setId(rs.getInt(1));
user.setName(rs.getString(2));
user.setEmail(rs.getString(3));
user.setQualification(rs.getString(5));
user.setRole(rs.getString(6));
}

I got following eror:

java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '? and password=?' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1200)

Solution and what is wrong?

for this line ResultSet rs=ps.executeQuery(sql); I do not need to add SQL query there.

I removed SQL keyword it code started working fine…

String sql = "select * from user where username=? and password=?";
PreparedStatement ps =connection.prepareStatement(sql);

ps.setString(1, username);
ps.setString(2, password);
ResultSet rs=ps.executeQuery();

SSL certificate integration in Spring boot Project

SSL certificate integration

SSL certificate setting applied in application.properties file To generate self signed SSL certificate use following command:

cd src/main/resources
keytool -genkeypair -alias local_ssl -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore local-ssl.p12 -validity 365 -ext san=dns:localhost

Put following code into application.properties file

spring.application.name=ssl-certificate
server.port=8443
#server.port=8080
server.ssl.enabled: true
server.ssl.key-alias: local_ssl
server.ssl.key-store: classpath:local-ssl.p12
server.ssl.key-store-type: PKCS12
server.ssl.key-password: 12345678
server.ssl.key-store-password: 12345678

Open this URL:
https://localhost:8443/

For production use following command and use p12 file in application Convert .crt to .p12

openssl pkcs12 -export -out server.p12 -inkey server.key -in server.crt

Where server.key , is the server key . server.crt is cert file from CA or self sign

you can find source code here:

https://github.com/purab/ssl-certificate-spring-boot

Jenkins installation on ubuntu OS

Use following command for jenkins installtion

  • sudo apt update
  • sudo apt install openjdk-8-jdk
  • apt install openjdk-8-jdk -y
  • wget -q -O – https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add –
  • sudo sh -c ‘echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list’
  • apt-get update
  • -sudo apt-get install jenkins -y
  • systemctl status jenkins
  • sudo ufw allow 8080
  • give sudo permisssion to jenkins user < /br> vi /etc/sudoers and add below entry jenkins ALL=(ALL) NOPASSWD: ALL and save the file

If you are getting this error

https://pkg.jenkins.io/debian-stable binary/ Release Certificate verification failed:

Then run following command

sudo su

apt update

apt-get install jenkins -y

Solved Error executing DDL “alter table events drop foreign key

While running spring boot application I got following error

2021-10-22 13:38:59.732  WARN 700 --- [ restartedMain] o.h.t.s.i.ExceptionHandlerLoggedImpl  : GenerationTarget encountered exception accepting command : Error executing DDL "alter table users_roles add constraint FKt4v0rrweyk393bdgt107vdx0x foreign key (role_id) references role (id)" via JDBC Statement

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table users_roles add constraint FKt4v0rrweyk393bdgt107vdx0x foreign key (role_id) references role (id)" via JDBC Statement

Changed spring.jpa.hibernate.ddl-auto = create-drop to update. And changed table name user to users and role to roles.

Then problem has been solved

Spring and Gradle – adding to the project WebJars Bootstrap

If you wanted to add bootstrap UI CSS and JS files in thymeleaf then you can use following code:

<link th:href="@{/webjars/bootstrap/5.0.0/css/bootstrap.min.css}"
rel="stylesheet" media="screen" />
<script th:src="@{/webjars/bootstrap/5.0.0/js/bootstrap.min.js}"></script>

If those styles are still not loading then refresh project and if you are using spring security then you need to add following code in your extended class of

WebSecurityConfigurerAdapter
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home","/webjars/**", "/resources/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}

It will solve your issue

auto renew letsencrypt ssl apache on linux

For auto renew SSL certificate I added following code in crontab.

0 0 */10 * * certbot renew >> /logs/certbot-cron.log 2>&1

Log of certbot renew command.

[root@vps147238 ep]# certbot renew

Saving debug log to /var/log/letsencrypt/letsencrypt.log Processing /etc/letsencrypt/renewal/purabtech.com.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Cert not yet due for renewal - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - The following certificates are not due for renewal yet: /etc/letsencrypt/live/purabtech.com/fullchain.pem expires on 2022-01-17 (skipped) (skipped) No renewals were attempted.