how to display the database name and change the database properties in java (spring framework) - database

I have a requirement to display the database name on the screen that is connected by the web application (which is configured through the datasource on weblogic server with spring data xml having all the configurations) and also is there any way to switch from current database to different datasource (database server) while working/running on the web application (with user screen).
Thanks.

As far as I know, there is no straight-forward way to know this info. One way I can think of doing this is to parse the spring config XML file using an XML parser for the desired element - even that too will only give you the JNDI name of the datasource your app would be using. I hope you have a mechanism to determine which JDBC JNDI name maps to which database. If you don't have that information, you would have to use JMX (MBeans) to connect to the Weblogic environment to get that info.

Related

Intellij embedded H2 database tables do not appear

I'm creating a Spring Boot application and I'm using Intellij's embedded h2 database.
I have added the following lines in my application.properties file:
spring.datasource.url=jdbc:h2:~/testdb;MV_STORE=false;AUTO_SERVER=TRUE
This is my data source configuration
Although the connection is successful and I can query the database using Intellij's query console, the tables do not appear in the Database tab.
Succeeded
DBMS: H2 (ver. 2.1.210 (2022-01-17))
Case sensitivity: plain=upper, delimited=exact
Driver: H2 JDBC Driver (ver. 2.1.210 (2022-01-17), JDBC4.2)
Ping: 16 ms
When I refresh the connection or go to the schemas tab of the data source configuration, I get the following error:
[42S02][42102] org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "INFORMATION_SCHEMA_CATALOG_NAME" not found; SQL statement: select CATALOG_NAME from INFORMATION_SCHEMA.INFORMATION_SCHEMA_CATALOG_NAME [42102-210].
By going to the advanced tab of the data source and clicking on expert options, we are presented with a checkbox labeled "Introspect using JDBC metadata"
By checking that box, the tables successfully appear in the Database tab
Regarding why this works, this is taken from the official documentation:
https://www.jetbrains.com/help/datagrip/data-sources-and-drivers-dialog.html
Introspect using JDBC metadata
Switch to the JDBC-based introspector. Available for all the databases.
To retrieve information about database objects (DB metadata), DataGrip uses the following introspectors:
A native introspector (might be unavailable for certain DBMS). The native introspector uses DBMS-specific tables and views as a source of metadata. It can retrieve DBMS-specific details and produce a more precise picture of database objects.
A JDBC-based introspector (available for all the DBMS). The JDBC-based introspector uses the metadata provided by the JDBC driver. It can retrieve only standard information about database objects and their properties.
Consider using the JDBC-based intorspector when the native introspector fails or is not available.
The native introspector can fail, when your database server version is older than the minimum version supported by DataGrip.
You can try to switch to the JDBC-based introspector to fix problems with retrieving the database structure information from your database. For example, when the schemas that exist in your database or database objects below the schema level are not shown in the Database tool window.

Microsoft SQL JDBC Spring boot configuration does not work when database name is not in the url

I have a spring boot JDBC Cofiguration A as below which works well,
Configuration A
spring.datasource.sqlserver.url=jdbc:sqlserver://localhost:1433;database=my-testdb
spring.datasource.sqlserver.user=user_101#server-product1
spring.datasource.sqlserver.password=password_101
spring.datasource.sqlserver.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
Configuration B below does not. Note that the url string does not have the database name in it. The database name is specified in a separate line, as one would expect to configure.
spring.datasource.sqlserver.url=jdbc:sqlserver://localhost:1433
spring.datasource.sqlserver.user=user_101#server-product1
spring.datasource.sqlserver.password=password_101
spring.datasource.sqlserver.database=my-testdb
spring.datasource.sqlserver.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
Cannot figure out why the database name cannot be specified in a separate line and has to be in the URL. Can anyone mention why this does not work?
Other configuration : Spring Boot, sqljdbc41.jar
Well that's how the Microsoft's SQL Server / Azure JDBC driver works, database name is a mandatory part of the URL, and not given in a "database" attribute.
Technically, it may be related to the fact that databases in SQL Server behave rather autonomously, they each have their own set of catalog views, you cannot switch between databases freely etc.

How to connect sql server with swift

I'm working now on an application for iOS (using swift), the database is already exist in SQL Server.
How I will use it and connect with it? Do i need a web service to do that?
thanks all .
It is recommended to use a web service since having the application talk directly to the database means you need to include the SQL Credentials in the binary and anyone with a copy of the application can get them and do whatever they wish in the database. From a security point of view, this is bad.
The correct approach is to have a web server which will host an "API" -- a web application that will receive HTTP requests from the app and translate them to database queries and then will return the response in another format, such as JSON.
However, you need to be careful. This web services must use HTTPS and must first validate the input in order to protect against attacks such as SQL Injection.

Can I use SignalR with SQL Server Backplace on an existing entity code first database?

According to Scaleout with SQL Server you can use SignalR.SqlServer to keep SignalR synced on a load balancing setup. I have an existing MVC 5 website with its own database created using Entity Code First. The article seems to use a dedicated database with service broker enabled and says not to modify the database.
So do I need to have a separate database for this? I know Entity can be picky if the database schema doesn't match and I worry that if I try to use the SignalR Sql Server package with the existing database that the tables it creates will cause a context changed error.
Also can someone provide me with more information about using the Microsoft.AspNet.SignalR.SqlServer package. The article I linked doesn't give a ton of detail and I don't know if I need to change anything in my hub and groups or if it is all handled automatically.
You should be able to, though you'd likely want to separate your entity framework definitions from signalR. You can either put SignalR in a separate database, or give the two a separate schema.
In terms of configuration, you'll need to make an addition to the Startup class of your web project:
public class Startup
{
public void Configuration(IAppBuilder app)
{
var sqlConnectionString = "connection string here";
GlobalHost.DependencyResolver.UseSqlServer(sqlConnectionString);
this.ConfigureAuth(app);
app.MapSignalR();
}
}

hibernate, server specific configuration

we have two different environments (test and production), each with it's own database connection.
The database connection is configured in the hibernate.cfg.xml, together with the mappings etc.
The hibernate.cfg.xml is part of the application - hence we cannot configure the database connection depending on the environment.
So we need some kind of configuration outside the application.
What is the best way for handling server specific database configurations with hibernate?
U can configure using annotations. below is exa
sessionFactory = new AnnotationConfiguration()
.addPackage("test.animals") //the fully qualified package name
.addAnnotatedClass(Flight.class)
.addAnnotatedClass(Sky.class)
.addAnnotatedClass(Person.class)
.addAnnotatedClass(Dog.class)
.addResource("test/animals/orm.xml")
.configure()
.buildSessionFactory();

Resources