java style database connection on android - java

package com.example.wgame;
import java.sql.Connection;
import java.sql.DriverManager;
import android.util.Log;
public class connDB {
Connection conn = null;
public static Connection connectDB() {
try{
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:wSdict.sqlite");
Log.v("DBCon","Connected!");
return conn;
}catch (Exception r) {
Log.e("DBCon","Not Connected");
return null;
}
}
}
is it possible to connect my sqlite database file this way on android and not having to use SQLiteHelper class ?

No you can not. Android uses sqlite3, and can only be accessed by the API provided by android. There is no third party library or any helper class to achieve this.
refer this link for more info.
http://developer.android.com/reference/android/database/package-summary.html

Related

how can i connect java to sqlite db browser

I am trying to connect sqlite db browser with java using nano editor i am very new here. i have
followed some youtube videos but i am stacking at mid can anyone please help
here is my code.
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SqliteDB{
Connection c = null;
Statement stmt = null;
SqliteDB(){
try{
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:signup.db");
System.out.println("Connected to DB");
}
catch(Exception e){
System.out.println("Error: "+ e.getMessage());
}
}
}
Error: org.sqlite.JDBC
thank you guys for helping.
I think that you have not imported library org.sqlite.JDBC in your project, Class.forName() return class that exists, when you get error like this it says that this class is not exists.
First of all download library from: https://bitbucket.org/xerial/sqlite-jdbc/downloads/
Then import in your project and that's how you should write the code:
public static void main(String[] args) throws SQLException {
org.sqlite.JDBC j = new org.sqlite.JDBC();
Connection c = DriverManager.getConnection("jdbc:sqlite:signup.db");
//do stuff here
}

NonWritableChannelException when trying to update an Access database

Using UCanAccess for the first time for a project and I am having a lot of trouble inserting a row into one of my database tables (in Microsoft Access).
My code makes sense but once I execute I end up getting the same error every time, even though NetBeans is able to connect to my database.
package Vegan;
import java.sql.Connection;
import java.sql.DriverManager;
public class connectionString {
static Connection connection = null;
public static Connection getConnection()
{
try
{
connection = DriverManager.getConnection("jdbc:ucanaccess://C://MyDatabase1.accdb");
System.out.println("---connection succesful---");
}
catch (Exception ex)
{
System.out.println("Connection Unsuccesful");
}
return connection;
}
package Vegan;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DB {
private static ResultSet rs = null;
private static PreparedStatement ps = null;
private static Connection connection = null;
public DB() {
connection = connectionString.getConnection();
}
public void AddTest() {
try {
String sql = "INSERT INTO CategoryTbl(CategoryName) VALUES (?)";
ps = connection.prepareStatement(sql);
ps.setString(1, "Flours");
ps.executeUpdate();
System.out.println("Inserted");
} catch (Exception ex) {
System.out.println(ex.getLocalizedMessage().toString());
}
}
After that, when I execute the the AddTest() method, I get this system output:
run:
---connection succesful---
java.nio.channels.NonWritableChannelException
at sun.nio.ch.FileChannelImpl.write(FileChannelImpl.java:724)
at com.healthmarketscience.jackcess.impl.PageChannel.writePage(PageChannel.java:297)
UCAExc:::3.0.6 null
at com.healthmarketscience.jackcess.impl.PageChannel.writePage(PageChannel.java:234)
at com.healthmarketscience.jackcess.impl.TableImpl.writeDataPage(TableImpl.java:1375)
at com.healthmarketscience.jackcess.impl.TableImpl.addRows(TableImpl.java:1624)
at com.healthmarketscience.jackcess.impl.TableImpl.addRow(TableImpl.java:1462)
at net.ucanaccess.converters.UcanaccessTable.addRow(UcanaccessTable.java:44)
at net.ucanaccess.commands.InsertCommand.insertRow(InsertCommand.java:101)
at net.ucanaccess.commands.InsertCommand.persist(InsertCommand.java:148)
at net.ucanaccess.jdbc.UcanaccessConnection.flushIO(UcanaccessConnection.java:315)
at net.ucanaccess.jdbc.UcanaccessConnection.commit(UcanaccessConnection.java:205)
at net.ucanaccess.jdbc.AbstractExecute.executeBase(AbstractExecute.java:161)
at net.ucanaccess.jdbc.ExecuteUpdate.execute(ExecuteUpdate.java:50)
at net.ucanaccess.jdbc.UcanaccessPreparedStatement.executeUpdate(UcanaccessPreparedStatement.java:253)
at Vegan.DB.AddTest(DB.java:91)
at Vegan.TestDB.main(TestDB.java:17)
BUILD SUCCESSFUL (total time: 1 second)
With no changes being made to the database when I check on it again Access.
What could be causing this, and what does the error message mean? Thank you
"java.nio.channels.NonWritableChannelException" means that the database file cannot be updated. In your case that was because the database file was in the root folder of the Windows system drive (C:\) and mere mortals have restricted permissions on that folder.
Solution: Move the database file to a folder where you have full write access.

make a connection from my program to database

I am new to programming and i am trying to make a connection from my program to database but when i make the code,error occurred on line 34 (package con.msql.jdbc does not exit).Can u tell me why??help me
code :
package Absensi_PEgawai;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class koneksi {
private static Connection koneksi;
public static Connection getKoneksi()
{
//cek koneksi
if(koneksi == null)
{
try
{
String url;
url = "jdbc:mysql://localhost:3306/dbabsensipegawai";
String user = "root";
String password = "pk12basketboy";
line34 --> **DriverManager.registerDriver(new con.mysql.jdbc.Driver());**
koneksi = DriverManager.getConnection(url,user,password);
}catch(SQLException t)
{
JOptionPane.showMessageDialog(null, "Error Membuat Koneksi");
}
}
return koneksi;
}
static Object getConnection()
{
throw new UnsupportedOperationException("Not Yet Implemented");
}
}
You must have forgotten to add the database driver to the CLASSPATH. If you are under Eclipse, go to the Build path menu and add your driver, otherwise refer to the billion posts on the internet that explain how to add a class to the CLASSPATH for any system.

In my application in java database connection code is not working

In this code class.forname it show error that class not found
so help me with this code because my is a local database application
I am using sqlite database.
package appview;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Databaseconnection {
Connection connection = null;
public static Connection connection2()
{
try
{
Class.forName("org.sqlite.JDBC");
Connection connection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Jainam\\Java Application\\JPH\\src\\database\\jph_db.sqlite");
return connection;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}
You have to declare the url this way: jdbc:sqlite:/DRIVE:/dirA/dirB/dbfile
DriverManager.getConnection("jdbc:sqlite:/C:/Users/Jainam/Java Application/JPH/src/database/jph_db.sqlite");
See Here for more informations
ClassNotFoundException means Java is not able to find the org.sqlite.JDBC class. You need to Download sqlite-jdbc-3.6.20.1.jar and put it in your class-path before running it.
here check database connectivity.
class not found ? then go for DDMS->FILE EXP-> SDCARD -> INSERT UR SQLITE.DB file.It will run proper.

Connect to MySQL using JDBC driver through a proxy [duplicate]

This question already has an answer here:
MySQL Connect via proxy in Java
(1 answer)
Closed 2 years ago.
In Java, I would like to make a connection to a MySQL server which is on the web from a client computer that is behind a http proxy. I have read few solutions some say http tunnelling might work and some suggest a very old link from oracle which is not available anymore. So the question is:
How can we connect to a MySQL server from a computer which is behind a http proxy?
You can try the following code and see if it works. It worked for me over TCP
package indika.jdbc.connectivity;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class ConnectOverProxy {
public static void main(String[] args) {
new ConnectOverProxy();
}
public ConnectOverProxy() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
Properties info = new Properties();
//info.put("proxy_type", "4"); // SSL Tunneling
info.put("proxy_host", "[proxy host]");
info.put("proxy_port", "[proxy port]");
info.put("proxy_user", "[proxy user]");
info.put("proxy_password", "[proxy password]");
info.put("user", "[db user]");
info.put("password", "[db pass word]");
conn = DriverManager.getConnection("jdbc:mysql://[db host]/",info);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("Select NOW()");
rs.next();
System.out.println("Data- " + rs.getString(1));
rs.close();
stmt.close();
conn.close();
} catch (SQLException er) {
er.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Also look at "http://www.idssoftware.com/jdbchttps.html", However I have not used this personally.

Categories