how can i connect android application to mysql using jsp? - java

I want to use jsp rather than php. Is it by jdbc driver? And what are the steps.
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://<server>/<database>",
<username>,
<password>);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT access_name, access_code FROM user_login");
while (rs.next()) {
x = rs.getString("access_name");
y = rs.getString("access_code");
}
This code gives me an exception like ClassNotFoundException com.mysql.jdbc.driver

You should download MySQL Connector from this link :
https://dev.mysql.com/downloads/connector/j/5.0.html .
Then just add it to your classpath.

Related

Moving jdbc connection to property files in JSP

I am bit new to JSP and JDBC, I want to know how to move a JDBC connection like below to a property file for example to test.properties
Class.forName("oracle.jdbc.driver.OracleDriver");
//Connection conn = DriverManager.getConnection("jdbc:oracle:thin:#nyvm0467.ptc.un.org:1521:EIDMSUAT", "DBO_EIDMSUAT", "NewPassDBO_EIDMSUAT");
Connection conn = DriverManager.getConnection("jdbc:", "ROD", "DMSP");
Statement statement = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY );
String queryString = "select area from helparea order by area";
ResultSet rs = statement.executeQuery(queryString);
One of the possibilities is as follows. correct me if I am wrong
ResourceBundle resource = ResourceBundle.getBundle("C:\\Users\\S.Mandava\\workspace2\\Contactus\\src\\OIMConnection");
String name=resource.getString("dbname");
String surname=resource.getString("dbpassword");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn2 = DriverManager.getConnection("jdbc:oracle:thin:#eidms-db-003.un.org:1521:EIDPMDM", resource.getString("dbname"), resource.getString("dbpassword"));
Statement statement2 = conn2.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY );
String queryString2 = "select area from helparea order by area";
ResultSet rs2 = statement2.executeQuery(queryString2);

hsqldb 2.3.2 jdbc driver does not support ResultSet.first()?

I am playing with HSQLDB+JDBC driver using JDK 8.
Using rs.next() looping results works fine, however, using rs.first() does not work: feature is not supported ?! Is is by design or a bug?
I plan to access hsqldb using Spring jdbc template, and I am concerned that I may stuck if I encounter such issue later on.
String jdbcUrl = "jdbc:hsqldb:hsql://localhost:9999/configdb";
try(Connection con = DriverManager.getConnection(jdbcUrl, "SA", "");
PreparedStatement stmt = con.prepareStatement(
"SELECT * FROM contacts");
) {
ResultSet rs = stmt.executeQuery();
// rs.first() does not work !
while(rs.next()){
//do sth here
}
} catch (SQLException e) {
throw new RuntimeException("test jdbc connection failed", e);
}
Try to make your ResultSet scrollable:
PreparedStatement stmt= conn.prepareStatement("SELECT * FROM contacts",
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
I think that should probably work

Create a database with username and password in Java

How to create a database with username and password in Java?
I created a DB using
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/", USER, PASS);
stmt = conn.createStatement();
stmt.executeUpdate("CREATE DATABASE "+DB_NAME;);
// DB creatd
}
catch(Exception e) {
e.printStackTrace();
}
but now if I connect to this database using:
Connection conn1 = DriverManager.getConnection("jdbc:mysql://localhost/"+DBNAME,user,pass);
what should I enter in the use and pass?
And more importantly how do I create a DB with properties?
One has to have very good reason to create a database via code but You would enter the same login credential as the one that created the database:
DriverManager.getConnection("jdbc:mysql://localhost/"+DBNAME, USER, PASS);
Alternatively once connected to the server you can connect to the database by running following query:
USE DB_NAME
Connection conn1=DriverManager.getConnection("jdbc:mysql://localhost/",user,pass);
stmt = conn1.createStatement();
String sql = "CREATE DATABASE UTSAV";
stmt.executeUpdate(sql);

No Database selected when retrieving from mysql website

I have a mysql database that I am trying to retrieve from our website host (godaddy). I followed a format that seems to be right but it tells me that:
java.sql.SQLException: No database selected
Code:
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// STEP 2: Register JDBC driver
Class.forName(JDBC_DRIVER);
// STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM item_master";
ResultSet rs = stmt.executeQuery(sql); //<-- This is where the error is.
// STEP 5: Extract data from result set
while (rs.next()) {
// Retrieve by column name
int id = rs.getInt("id");
// Display values
System.out.print("ID: " + id);
}
...
}
I did a print statement of the conn to maybe think the connection could of been null and was shown this:
com.mysql.jdbc.JDBC4Connection#2a6*****
Anyone have any ideas what could cause something like this?
Your URL for your database should include your database name. This is normally your URL followed by a "/DBNAME".
String URL = "jdbc:mysql://localhost:3306/mydb";
Where "mydb" is your database name.
What is the value of DB_URL? As far as I am aware, the URL needs to be of the form:
"WEBURL/DATABASENAME:PORT"
Are you just trying to connect to the WEBURL, without specifying a DATABASE?
I had the same problem.
Just run one more query (before your 'SELECT' statement) to connect to your database.
First:
sql = "USE <yourdatabase>";
ResultSet rs = stmt.executeQuery(sql);
And after:
sql = "SELECT * FROM item_master";
rs = stmt.executeQuery(sql);

Can't read tables from firefox using Java sqlite jdbc

I'm using the following code to connect and read moz_bookmarks from Java
String connection = "jdbc:sqlite:/" + Tracking.FILES_LOCATION + "places.sqlite";
Connection conn1 = DriverManager.getConnection("jdbc:sqlite:path_to_places.sqlite/");
Statement stat = conn1.createStatement();
ResultSet rs1 = stat.executeQuery("select * from moz_bookmarks;");
But I keep getting : "java.sql.SQLException: file is encrypted or is not a database"
Even after upgrading my jdbc sqlite driver for sqlite3
Any help is greatly appreciated
Thanks!
You create a connection name in first line, but in the second line you have a connection string which is probably copied from some tutorial.
Try to use connection variable when calling getConnection method.
Try this:
String connection = "jdbc:sqlite:/" + Tracking.FILES_LOCATION + "places.sqlite";
Connection conn1 = DriverManager.getConnection(connection);
Statement stat = conn1.createStatement();
String Query = "select * from moz_bookmarks";
ResultSet rs1 = stat.executeQuery(Query);

Categories

Resources