我的程序连接到MySQL数据库工作正常。 然后,不更改任何代码用于build立连接,我得到这个exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. 发生了什么? 用于获取连接的代码: private static Connection getDBConnection() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException { String username = "user"; String password = "pass"; String url = "jdbc:mysql://www.domain.com:3306/dbName?connectTimeout=3000"; Class.forName("com.mysql.jdbc.Driver"); Connection conn […]
代码有什么问题,debugging时有很多错误。 我正在写一个单身类的代码来连接数据库的MySQL。 这是我的代码 package com.glomindz.mercuri.util; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; public class MySingleTon { String url = "jdbc:mysql://localhost:3306/"; String dbName = "test"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = ""; private static MySingleTon myObj; private Connection Con ; private MySingleTon() { System.out.println("Hello"); Con= createConnection(); } @SuppressWarnings("rawtypes") public Connection […]
据说是使用后closures所有JDBC资源的好习惯。 但是,如果我有以下代码,是否有必要closures结果集和语句? Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { conn = // Retrieve connection stmt = conn.prepareStatement(// Some SQL); rs = stmt.executeQuery(); } catch(Exception e) { // Error Handling } finally { try { if (rs != null) rs.close(); } catch (Exception e) {}; try { if (stmt […]
我的桌子的结构: id int AUTO_INCREMENT PRIMARY KEY title text url text age int 以下是我试图将数据保存到此表中的方法: PreparedStatement ps=con.prepareStatement("insert into table(title, url, age) values ('\"+title+\",\"+url+\",\"+age+\"')"); System.out.println("Connected database successfully.."); ps.executeUpdate(); 但是当我运行该应用程序,我得到 java.sql.SQLException:列计数与第1行的值计数不匹配 我猜这个问题可能在id列中,如何解决呢?
为了改变这个问题:我应该避免共享在不同线程之间实现java.sql.Connection的类的实例吗?
我在GlassFish上有一个Java-JSF Web应用程序,我想在其中使用连接池。 因此,我创build了一个application范围的bean,为其他bean的Connection实例提供服务: public class DatabaseBean { private DataSource myDataSource; public DatabaseBean() { try { Context ctx = new InitialContext(); ecwinsDataSource = (DataSource) ctx.lookup("jdbc/myDataSource"); } catch (NamingException ex) { ex.printStackTrace(); } } public Connection getConnection() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException { Connection connection = myDataSource.getConnection(); System.out.println("Succesfully connected: " + connection); //Sample: Succesfully connected: com.sun.gjc.spi.jdbc40.ConnectionHolder40@7fb213a5 return […]
在JDBC中是否有名称参数,而不是位置参数,比如下面的ADO.NET查询中的@city , @city ? select * from customers where name=@name and city = @city
我想知道是否有指定使用准备语句返回的列名称。 我正在使用MySQL和Java。 当我尝试它时: String columnNames="d,e,f"; //Actually from the user… String name = "some_table"; //From user… String query = "SELECT a,b,c,? FROM " + name + " WHERE d=?";//… stmt = conn.prepareStatement(query); stmt.setString(1, columnNames); stmt.setString(2, "x"); 我得到这种types的声明(执行之前打印)。 SELECT a,b,c,'d,e,f' FROM some_table WHERE d='x' 不过我想看看: SELECT a,b,c,d,e,f FROM some_table WHERE d='x' 我知道,我不能这样做表名,正如这里所讨论的,但是想知道是否有某种方法可以对列名进行操作。 如果没有,那么我将不得不尝试确保我对input进行清理,以免导致SQL注入漏洞。
我在Netbeans中得到这个错误: java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/ 这是如何造成的,我该如何解决?
我正在使用预处理语句来执行mysql数据库查询。 我想实现一个基于关键字的searchfunction。 为此我需要使用LIKE关键字,那我知道。 我之前也使用过预处理语句,但是我不知道如何将它用于LIKE因为从下面的代码中我将添加'keyword%' ? 我可以直接在pstmt.setString(1, notes)使用它(1, notes+"%")或类似的东西。 我在网上看到很多post,但没有任何好的答案。 PreparedStatement pstmt = con.prepareStatement( "SELECT * FROM analysis WHERE notes like ?"); pstmt.setString(1, notes); ResultSet rs = pstmt.executeQuery();